]> git.sesse.net Git - nageru/blob - nageru/flags.cpp
Small consistency fix in --help.
[nageru] / 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_FULLSCREEN,
18         OPTION_MAX_NUM_CARDS,
19         OPTION_MULTICHANNEL,
20         OPTION_MIDI_MAPPING,
21         OPTION_DEFAULT_HDMI_INPUT,
22         OPTION_FAKE_CARDS_AUDIO,
23         OPTION_V4L_OUTPUT,
24         OPTION_HTTP_UNCOMPRESSED_VIDEO,
25         OPTION_HTTP_X264_VIDEO,
26         OPTION_HTTP_AV1_VIDEO,
27         OPTION_RECORD_X264_VIDEO,
28         OPTION_SEPARATE_X264_DISK_ENCODE,
29         OPTION_X264_PRESET,
30         OPTION_X264_TUNE,
31         OPTION_X264_SPEEDCONTROL,
32         OPTION_X264_SPEEDCONTROL_VERBOSE,
33         OPTION_X264_BITRATE,
34         OPTION_X264_CRF,
35         OPTION_X264_VBV_BUFSIZE,
36         OPTION_X264_VBV_MAX_BITRATE,
37         OPTION_X264_PARAM,
38         OPTION_X264_SEPARATE_DISK_PRESET,
39         OPTION_X264_SEPARATE_DISK_TUNE,
40         OPTION_X264_SEPARATE_DISK_BITRATE,
41         OPTION_X264_SEPARATE_DISK_CRF,
42         OPTION_X264_SEPARATE_DISK_PARAM,
43         OPTION_AV1_PRESET,
44         OPTION_AV1_BITRATE,
45         OPTION_AV1_FPS,
46         OPTION_AV1_PARAM,
47         OPTION_HTTP_MUX,
48         OPTION_HTTP_COARSE_TIMEBASE,
49         OPTION_HTTP_AUDIO_CODEC,
50         OPTION_HTTP_AUDIO_BITRATE,
51         OPTION_HTTP_PORT,
52         OPTION_SRT_PORT,
53         OPTION_NO_SRT,
54         OPTION_NO_TRANSCODE_VIDEO,
55         OPTION_NO_TRANSCODE_AUDIO,
56         OPTION_DISABLE_AUDIO,
57         OPTION_FLAT_AUDIO,
58         OPTION_GAIN_STAGING,
59         OPTION_DISABLE_LOCUT,
60         OPTION_ENABLE_LOCUT,
61         OPTION_DISABLE_GAIN_STAGING_AUTO,
62         OPTION_ENABLE_GAIN_STAGING_AUTO,
63         OPTION_DISABLE_COMPRESSOR,
64         OPTION_ENABLE_COMPRESSOR,
65         OPTION_DISABLE_LIMITER,
66         OPTION_ENABLE_LIMITER,
67         OPTION_DISABLE_MAKEUP_GAIN_AUTO,
68         OPTION_ENABLE_MAKEUP_GAIN_AUTO,
69         OPTION_DISABLE_ALSA_OUTPUT,
70         OPTION_NO_FLUSH_PBOS,
71         OPTION_PRINT_VIDEO_LATENCY,
72         OPTION_MAX_INPUT_QUEUE_FRAMES,
73         OPTION_AUDIO_QUEUE_LENGTH_MS,
74         OPTION_OUTPUT_YCBCR_COEFFICIENTS,
75         OPTION_OUTPUT_BUFFER_FRAMES,
76         OPTION_OUTPUT_SLOP_FRAMES,
77         OPTION_OUTPUT_CARD_UNSYNCHRONIZED,
78         OPTION_TIMECODE_STREAM,
79         OPTION_TIMECODE_STDOUT,
80         OPTION_QUICK_CUT_KEYS,
81         OPTION_10_BIT,
82         OPTION_10_BIT_INPUT,
83         OPTION_10_BIT_OUTPUT,
84         OPTION_INPUT_YCBCR_INTERPRETATION,
85         OPTION_MJPEG_EXPORT_CARDS,
86 };
87
88 map<unsigned, unsigned> parse_mjpeg_export_cards(char *optarg)
89 {
90         map<unsigned, unsigned> ret;
91         if (optarg[0] == '\0') {
92                 return ret;
93         }
94
95         unsigned stream_idx = 0;
96         char *start = optarg;
97         for ( ;; ) {
98                 char *end = strchr(start, ',');
99                 if (end != nullptr) {
100                         *end = '\0';
101                 }
102
103                 unsigned range_begin, range_end;
104                 if (sscanf(start, "%u-%u", &range_begin, &range_end) != 2) {
105                         range_begin = range_end = atoi(start);
106                 }
107                 if (range_end < range_begin) {
108                         fprintf(stderr, "ERROR: Invalid range %u-%u in --mjpeg-export-cards=\n", range_begin, range_end);
109                         exit(1);
110                 }
111                 if (range_end >= unsigned(global_flags.max_num_cards)) {
112                         // There are situations where we could possibly want to
113                         // include FFmpeg inputs (CEF inputs are unlikely),
114                         // but they're not necessarily in 4:2:2 Y'CbCr, so it would
115                         // require more functionality the the JPEG encoder.
116                         fprintf(stderr, "ERROR: Asked for (zero-indexed) card %u in --mjpeg-export-cards=, but there are only %u cards\n",
117                                 range_end, global_flags.max_num_cards);
118                         exit(1);
119                 }
120                 for (unsigned card_idx = range_begin; card_idx <= range_end; ++card_idx) {
121                         if (ret.count(card_idx)) {
122                                 fprintf(stderr, "ERROR: Card %u was given twice in --mjpeg-export-cards=\n", card_idx);
123                                 exit(1);
124                         }
125                         ret[card_idx] = stream_idx++;
126                 }
127                 if (end == nullptr) {
128                         break;
129                 } else {
130                         start = end + 1;
131                 }
132         }
133         return ret;
134 }
135
136 void usage(Program program)
137 {
138         if (program == PROGRAM_KAERU) {
139                 fprintf(stderr, "Usage: kaeru [OPTION]... SOURCE_URL\n");
140         } else {
141                 fprintf(stderr, "Usage: nageru [OPTION]...\n");
142         }
143         fprintf(stderr, "\n");
144         fprintf(stderr, "      --help                      print usage information\n");
145         if (program == PROGRAM_NAGERU) {
146                 fprintf(stderr, "      --fullscreen                run in full screen, with no decorations\n");
147         }
148         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
149         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
150         if (program == PROGRAM_NAGERU) {
151                 fprintf(stderr, "  -c, --num-cards                 set minimum number of input cards (default 2)\n");
152                 fprintf(stderr, "      --max-num-cards             set maximum number of input cards (default %d)\n", MAX_VIDEO_CARDS);
153                 fprintf(stderr, "  -o, --output-card=CARD          also output signal to the given card (default none)\n");
154                 fprintf(stderr, "  -t, --theme=FILE                choose theme (default theme.lua)\n");
155                 fprintf(stderr, "  -I, --theme-dir=DIR             search for theme in this directory (can be given multiple times)\n");
156                 fprintf(stderr, "  -r, --recording-dir=DIR         where to store disk recording\n");
157                 fprintf(stderr, "  -v, --va-display=SPEC           VA-API device for H.264 encoding\n");
158                 fprintf(stderr, "                                    ($DISPLAY spec or /dev/dri/render* path)\n");
159                 fprintf(stderr, "  -m, --map-signal=SIGNAL,CARD    set a default card mapping (can be given multiple times)\n");
160                 fprintf(stderr, "  -M, --input-mapping=FILE        start with the given audio input mapping (implies --multichannel)\n");
161                 fprintf(stderr, "      --multichannel              start in multichannel audio mapping mode\n");
162                 fprintf(stderr, "      --midi-mapping=FILE         start with the given MIDI controller mapping (implies --multichannel)\n");
163                 fprintf(stderr, "      --default-hdmi-input        default to HDMI over SDI inputs for cards that have both\n");
164                 fprintf(stderr, "      --fake-cards-audio          make fake (disconnected) cards output a simple tone\n");
165                 fprintf(stderr, "      --v4l-output=DEVICE         send video (no audio) to V4L2 output/loopback\n");
166                 fprintf(stderr, "      --http-x264-video           send x264-compressed video to HTTP clients\n");
167                 fprintf(stderr, "      --record-x264-video         store x264-compressed video to disk (implies --http-x264-video,\n");
168                 fprintf(stderr, "                                    removes the need for working VA-API encoding)\n");
169                 fprintf(stderr, "      --separate-x264-disk-encode run a different x264 encoder for disk recording\n");
170                 fprintf(stderr, "                                    (implies --record-x264-video)\n");
171         }
172         fprintf(stderr, "      --x264-preset               x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
173         fprintf(stderr, "      --x264-tune                 x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
174         fprintf(stderr, "      --x264-speedcontrol         try to match x264 preset to available CPU speed\n");
175         fprintf(stderr, "      --x264-bitrate              x264 bitrate (in kilobit/sec, default %d)\n",
176                 DEFAULT_X264_OUTPUT_BIT_RATE);
177         fprintf(stderr, "      --x264-crf=VALUE            quality-based VBR (-12 to 51), incompatible with --x264-bitrate\n");
178         fprintf(stderr, "      --x264-param=NAME[,VALUE]   set any x264 parameter, for fine tuning\n");
179         if (program == PROGRAM_NAGERU) {
180                 fprintf(stderr, "      --x264-separate-disk-preset x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
181                 fprintf(stderr, "      --x264-separate-disk-tune   x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
182                 fprintf(stderr, "      --x264-separate-disk-bitrate  x264 bitrate (in kilobit/sec, default %d)\n",
183                         DEFAULT_X264_OUTPUT_BIT_RATE);
184                 fprintf(stderr, "      --x264-separate-disk-crf=VALUE  quality-based VBR (-12 to 51), \n");
185                 fprintf(stderr, "                                  incompatible with --x264-separate-disk-bitrate\n");
186                 fprintf(stderr, "      --x264-separate-disk-param=NAME[,VALUE] set any x264 parameter, for fine tuning\n");
187         }
188 #ifdef HAVE_AV1
189         fprintf(stderr, "      --http-av1-video            send AV1-compressed video to HTTP clients\n");
190         fprintf(stderr, "      --av1-preset                SVT-AV1 quality preset (default %d, from -2 to 13)\n",
191                 DEFAULT_AV1_PRESET);
192         fprintf(stderr, "      --av1-bitrate               AV1 bitrate (in kilobit/sec, default %d)\n",
193                 DEFAULT_AV1_OUTPUT_BIT_RATE);
194         fprintf(stderr, "      --av1-fps=NUM[/DEN]         AV1 encoded frame rate (default %d/%d)\n",
195                 DEFAULT_AV1_FPS_NUM, DEFAULT_AV1_FPS_DEN);
196         fprintf(stderr, "      --av1-param=NAME[,VALUE]    set any SVT-AV1 parameter, for fine tuning\n");
197 #endif
198         fprintf(stderr, "      --http-mux=NAME             mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
199         fprintf(stderr, "      --http-audio-codec=NAME     audio codec to use for HTTP streams\n");
200         fprintf(stderr, "                                  (default is to use the same as for the recording)\n");
201         fprintf(stderr, "      --http-audio-bitrate=KBITS  audio codec bit rate to use for HTTP streams\n");
202         fprintf(stderr, "                                  (default is %d, ignored unless --http-audio-codec is set)\n",
203                 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
204         fprintf(stderr, "      --http-port=PORT            which port to use for the built-in HTTP server\n");
205         fprintf(stderr, "                                  (default is %d)\n", DEFAULT_HTTPD_PORT);
206         fprintf(stderr, "      --srt-port=PORT             which port to use for receiving SRT streams\n");
207         fprintf(stderr, "                                  (default is %d)\n", DEFAULT_SRT_PORT);
208         fprintf(stderr, "      --no-srt                    disable receiving SRT streams\n");
209         if (program == PROGRAM_KAERU) {
210                 fprintf(stderr, "      --no-transcode-video        copy encoded video raw from the source stream\n");
211                 fprintf(stderr, "                                    (experimental, must be H.264)\n");
212                 fprintf(stderr, "      --no-transcode-audio        copy encoded audio raw from the source stream\n");
213                 fprintf(stderr, "                                    (requires --http-audio-codec= to be set)\n");
214                 fprintf(stderr, "      --disable-audio             do not include any audio in the stream\n");
215         }
216         if (program == PROGRAM_NAGERU) {
217                 fprintf(stderr, "      --flat-audio                start with most audio processing turned off\n");
218                 fprintf(stderr, "                                    (can be overridden by e.g. --enable-limiter)\n");
219                 fprintf(stderr, "      --gain-staging=DB           set initial gain staging to the given value\n");
220                 fprintf(stderr, "                                    (--disable-gain-staging-auto)\n");
221                 fprintf(stderr, "      --disable-locut             turn off locut filter (also --enable)\n");
222                 fprintf(stderr, "      --disable-gain-staging-auto  turn off automatic gain staging (also --enable)\n");
223                 fprintf(stderr, "      --disable-compressor        turn off regular compressor (also --enable)\n");
224                 fprintf(stderr, "      --disable-limiter           turn off limiter (also --enable)\n");
225                 fprintf(stderr, "      --disable-makeup-gain-auto  turn off auto-adjustment of final makeup gain (also --enable)\n");
226                 fprintf(stderr, "      --disable-alsa-output       disable audio monitoring via ALSA\n");
227                 fprintf(stderr, "      --print-video-latency       print out measurements of video latency on stdout\n");
228                 fprintf(stderr, "      --max-input-queue-frames=FRAMES  never keep more than FRAMES frames for each card\n");
229                 fprintf(stderr, "                                    (default 6, minimum 1)\n");
230                 fprintf(stderr, "      --audio-queue-length-ms=MS  length of audio resampling queue (default 100.0)\n");
231                 fprintf(stderr, "      --output-ycbcr-coefficients={rec601,rec709,auto}\n");
232                 fprintf(stderr, "                                  Y'CbCr coefficient standard of output (default auto)\n");
233                 fprintf(stderr, "                                    auto is rec601, unless --output-card is used\n");
234                 fprintf(stderr, "                                    and a Rec. 709 mode (typically HD modes) is in use\n");
235                 fprintf(stderr, "      --output-buffer-frames=NUM  number of frames in output buffer for --output-card,\n");
236                 fprintf(stderr, "                                    can be fractional (default 6.0); note also\n");
237                 fprintf(stderr, "                                    the audio queue can't be much longer than this\n");
238                 fprintf(stderr, "      --output-slop-frames=NUM    if more less than this number of frames behind for\n");
239                 fprintf(stderr, "                                    --output-card, try to submit anyway instead of\n");
240                 fprintf(stderr, "                                    dropping the frame (default 0.5)\n");
241                 fprintf(stderr, "      --output-card-unsynchronized  if --output-card is in use, do _not_ use it as\n");
242                 fprintf(stderr, "                                  master clock (may give jittery output and audio breakups)\n");
243                 fprintf(stderr, "      --timecode-stream           show timestamp and timecode in stream\n");
244                 fprintf(stderr, "      --timecode-stdout           show timestamp and timecode on standard output\n");
245                 fprintf(stderr, "      --quick-cut-keys            enable direct cutting by Q, W, E, ... keys\n");
246                 fprintf(stderr, "      --10-bit                    use 10-bit color depth\n");
247                 fprintf(stderr, "      --input-ycbcr-interpretation=CARD,{rec601,rec709,auto}[,{limited,full}]\n");
248                 fprintf(stderr, "                                  Y'CbCr coefficient standard of card CARD (default auto)\n");
249                 fprintf(stderr, "                                    auto is rec601 for SD, rec709 for HD, always limited\n");
250                 fprintf(stderr, "                                    limited means standard 0-240/0-235 input range (for 8-bit)\n");
251                 fprintf(stderr, "      --mjpeg-export-cards=RANGE[,RANGE...]\n");
252                 fprintf(stderr, "                                  export the given cards in MJPEG format to /multicam.mp4,\n");
253                 fprintf(stderr, "                                    in the given order (ranges can be either single card indexes\n");
254                 fprintf(stderr, "                                    or pairs like 1-3 for camera 1,2,3; default is all cards)\n");
255         }
256 }
257
258 void parse_flags(Program program, int argc, char * const argv[])
259 {
260         static const option long_options[] = {
261                 { "help", no_argument, 0, OPTION_HELP },
262                 { "fullscreen", no_argument, 0, OPTION_FULLSCREEN },
263                 { "width", required_argument, 0, 'w' },
264                 { "height", required_argument, 0, 'h' },
265                 { "num-cards", required_argument, 0, 'c' },
266                 { "max-num-cards", required_argument, 0, OPTION_MAX_NUM_CARDS },
267                 { "output-card", required_argument, 0, 'o' },
268                 { "theme", required_argument, 0, 't' },
269                 { "theme-dir", required_argument, 0, 'I' },
270                 { "recording-dir", required_argument, 0, 'r' },
271                 { "map-signal", required_argument, 0, 'm' },
272                 { "input-mapping", required_argument, 0, 'M' },
273                 { "va-display", required_argument, 0, 'v' },
274                 { "multichannel", no_argument, 0, OPTION_MULTICHANNEL },
275                 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
276                 { "default-hdmi-input", no_argument, 0, OPTION_DEFAULT_HDMI_INPUT },
277                 { "fake-cards-audio", no_argument, 0, OPTION_FAKE_CARDS_AUDIO },
278                 { "v4l-output", required_argument, 0, OPTION_V4L_OUTPUT },
279                 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
280                 { "record-x264-video", no_argument, 0, OPTION_RECORD_X264_VIDEO },
281                 { "separate-x264-disk-encode", no_argument, 0, OPTION_SEPARATE_X264_DISK_ENCODE },
282                 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
283                 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
284                 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
285                 { "x264-crf", required_argument, 0, OPTION_X264_CRF },
286                 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
287                 { "x264-separate-disk-preset", required_argument, 0, OPTION_X264_SEPARATE_DISK_PRESET },
288                 { "x264-separate-disk-tune", required_argument, 0, OPTION_X264_SEPARATE_DISK_TUNE },
289                 { "x264-separate-disk-bitrate", required_argument, 0, OPTION_X264_SEPARATE_DISK_BITRATE },
290                 { "x264-separate-disk-crf", required_argument, 0, OPTION_X264_SEPARATE_DISK_CRF },
291                 { "x264-separate-disk-param", required_argument, 0, OPTION_X264_SEPARATE_DISK_PARAM },
292 #ifdef HAVE_AV1
293                 { "http-av1-video", no_argument, 0, OPTION_HTTP_AV1_VIDEO },
294                 { "av1-preset", required_argument, 0, OPTION_AV1_PRESET },
295                 { "av1-bitrate", required_argument, 0, OPTION_AV1_BITRATE },
296                 { "av1-fps", required_argument, 0, OPTION_AV1_FPS },
297                 { "av1-param", required_argument, 0, OPTION_AV1_PARAM },
298 #endif
299                 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
300                 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
301                 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
302                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
303                 { "srt-port", required_argument, 0, OPTION_SRT_PORT },
304                 { "no-srt", no_argument, 0, OPTION_NO_SRT },
305                 { "no-transcode-video", no_argument, 0, OPTION_NO_TRANSCODE_VIDEO },
306                 { "no-transcode-audio", no_argument, 0, OPTION_NO_TRANSCODE_AUDIO },
307                 { "disable-audio", no_argument, 0, OPTION_DISABLE_AUDIO },
308                 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
309                 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
310                 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
311                 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
312                 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
313                 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
314                 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
315                 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
316                 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
317                 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
318                 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
319                 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
320                 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
321                 { "print-video-latency", no_argument, 0, OPTION_PRINT_VIDEO_LATENCY },
322                 { "max-input-queue-frames", required_argument, 0, OPTION_MAX_INPUT_QUEUE_FRAMES },
323                 { "audio-queue-length-ms", required_argument, 0, OPTION_AUDIO_QUEUE_LENGTH_MS },
324                 { "output-ycbcr-coefficients", required_argument, 0, OPTION_OUTPUT_YCBCR_COEFFICIENTS },
325                 { "output-buffer-frames", required_argument, 0, OPTION_OUTPUT_BUFFER_FRAMES },
326                 { "output-slop-frames", required_argument, 0, OPTION_OUTPUT_SLOP_FRAMES },
327                 { "output-card-unsynchronized", no_argument, 0, OPTION_OUTPUT_CARD_UNSYNCHRONIZED },
328                 { "timecode-stream", no_argument, 0, OPTION_TIMECODE_STREAM },
329                 { "timecode-stdout", no_argument, 0, OPTION_TIMECODE_STDOUT },
330                 { "quick-cut-keys", no_argument, 0, OPTION_QUICK_CUT_KEYS },
331                 { "10-bit", no_argument, 0, OPTION_10_BIT },
332                 { "input-ycbcr-interpretation", required_argument, 0, OPTION_INPUT_YCBCR_INTERPRETATION },
333                 { "mjpeg-export-cards", required_argument, 0, OPTION_MJPEG_EXPORT_CARDS },
334
335                 // Do not explicitly signal texture data uploads (will give
336                 // display corruption, but makes it possible to run with
337                 // apitrace in real time). For developers only, so undocumented.
338                 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
339
340                 // Output speedcontrol debugging statistics. For developers only,
341                 // so undocumented (users can get this information from Prometheus
342                 // statistics, too).
343                 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
344
345                 // Deprecated variants.
346                 { "10-bit-input", no_argument, 0, OPTION_10_BIT_INPUT },
347                 { "10-bit-output", no_argument, 0, OPTION_10_BIT_OUTPUT },
348
349                 { 0, 0, 0, 0 }
350         };
351         vector<string> theme_dirs;
352         string output_ycbcr_coefficients = "auto";
353         bool card_to_mjpeg_stream_export_set = false;
354         for ( ;; ) {
355                 int option_index = 0;
356                 int c = getopt_long(argc, argv, "c:o:t:I:r:v:m:M:w:h:", long_options, &option_index);
357
358                 if (c == -1) {
359                         break;
360                 }
361                 switch (c) {
362                 case 'w':
363                         global_flags.width = atoi(optarg);
364                         break;
365                 case 'h':
366                         global_flags.height = atoi(optarg);
367                         break;
368                 case 'c':
369                         global_flags.min_num_cards = atoi(optarg);
370                         break;
371                 case OPTION_MAX_NUM_CARDS:
372                         global_flags.max_num_cards = atoi(optarg);
373                         break;
374                 case 'o':
375                         global_flags.output_card = atoi(optarg);
376                         break;
377                 case 't':
378                         global_flags.theme_filename = optarg;
379                         break;
380                 case 'I':
381                         theme_dirs.push_back(optarg);
382                         break;
383                 case 'r':
384                         global_flags.recording_dir = optarg;
385                         break;
386                 case 'm': {
387                         char *ptr = strchr(optarg, ',');
388                         if (ptr == nullptr) {
389                                 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
390                                 exit(1);
391                         }
392                         *ptr = '\0';
393                         const int signal_num = atoi(optarg);
394                         const int card_num = atoi(ptr + 1);
395                         if (global_flags.default_stream_mapping.count(signal_num)) {
396                                 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
397                                         signal_num, global_flags.default_stream_mapping[signal_num]);
398                                 exit(1);
399                         }
400                         global_flags.default_stream_mapping[signal_num] = card_num;
401                         break;
402                 }
403                 case 'M':
404                         global_flags.input_mapping_filename = optarg;
405                         break;
406                 case OPTION_MULTICHANNEL:
407                         global_flags.multichannel_mapping_mode = true;
408                         break;
409                 case 'v':
410                         global_flags.va_display = optarg;
411                         break;
412                 case OPTION_MIDI_MAPPING:
413                         global_flags.midi_mapping_filename = optarg;
414                         global_flags.multichannel_mapping_mode = true;
415                         break;
416                 case OPTION_DEFAULT_HDMI_INPUT:
417                         global_flags.default_hdmi_input = true;
418                         break;
419                 case OPTION_FAKE_CARDS_AUDIO:
420                         global_flags.fake_cards_audio = true;
421                         break;
422                 case OPTION_V4L_OUTPUT:
423                         global_flags.v4l_output_device = optarg;
424                         break;
425                 case OPTION_HTTP_MUX:
426                         global_flags.stream_mux_name = optarg;
427                         break;
428                 case OPTION_HTTP_AUDIO_CODEC:
429                         global_flags.stream_audio_codec_name = optarg;
430                         break;
431                 case OPTION_HTTP_AUDIO_BITRATE:
432                         global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
433                         break;
434                 case OPTION_HTTP_PORT:
435                         global_flags.http_port = atoi(optarg);
436                         break;
437                 case OPTION_SRT_PORT:
438                         global_flags.srt_port = atoi(optarg);
439                         break;
440                 case OPTION_NO_SRT:
441                         global_flags.srt_port = -1;
442                         break;
443                 case OPTION_NO_TRANSCODE_VIDEO:
444                         global_flags.transcode_video = false;
445                         break;
446                 case OPTION_NO_TRANSCODE_AUDIO:
447                         global_flags.transcode_audio = false;
448                         break;
449                 case OPTION_DISABLE_AUDIO:
450                         global_flags.transcode_audio = false;
451                         global_flags.enable_audio = false;
452                         break;
453                 case OPTION_HTTP_X264_VIDEO:
454                         global_flags.x264_video_to_http = true;
455                         break;
456                 case OPTION_RECORD_X264_VIDEO:
457                         global_flags.x264_video_to_disk = true;
458                         break;
459                 case OPTION_SEPARATE_X264_DISK_ENCODE:
460                         global_flags.x264_video_to_disk = true;
461                         global_flags.x264_video_to_http = true;
462                         global_flags.x264_separate_disk_encode = true;
463                         break;
464                 case OPTION_HTTP_AV1_VIDEO:
465                         global_flags.av1_video_to_http = true;
466                         break;
467                 case OPTION_X264_PRESET:
468                         global_flags.x264_preset = optarg;
469                         break;
470                 case OPTION_X264_TUNE:
471                         global_flags.x264_tune = optarg;
472                         break;
473                 case OPTION_X264_SPEEDCONTROL:
474                         global_flags.x264_speedcontrol = true;
475                         break;
476                 case OPTION_X264_SPEEDCONTROL_VERBOSE:
477                         global_flags.x264_speedcontrol_verbose = true;
478                         break;
479                 case OPTION_X264_BITRATE:
480                         global_flags.x264_bitrate = atoi(optarg);
481                         break;
482                 case OPTION_X264_CRF:
483                         global_flags.x264_crf = atof(optarg);
484                         break;
485                 case OPTION_X264_PARAM:
486                         global_flags.x264_extra_param.push_back(optarg);
487                         break;
488                 case OPTION_X264_SEPARATE_DISK_PRESET:
489                         global_flags.x264_separate_disk_preset = optarg;
490                         break;
491                 case OPTION_X264_SEPARATE_DISK_TUNE:
492                         global_flags.x264_separate_disk_tune = optarg;
493                         break;
494                 case OPTION_X264_SEPARATE_DISK_BITRATE:
495                         global_flags.x264_separate_disk_bitrate = atoi(optarg);
496                         break;
497                 case OPTION_X264_SEPARATE_DISK_CRF:
498                         global_flags.x264_separate_disk_crf = atof(optarg);
499                         break;
500                 case OPTION_X264_SEPARATE_DISK_PARAM:
501                         global_flags.x264_separate_disk_extra_param.push_back(optarg);
502                         break;
503                 case OPTION_AV1_PRESET:
504                         global_flags.av1_preset = atoi(optarg);
505                         break;
506                 case OPTION_AV1_BITRATE:
507                         global_flags.av1_bitrate = atoi(optarg);
508                         break;
509                 case OPTION_AV1_FPS: {
510                         string str = optarg;
511                         const size_t pos = str.find('/');
512                         if (pos == string::npos) {
513                                 global_flags.av1_fps_num = stoi(str);
514                                 global_flags.av1_fps_den = 1;
515                         } else {
516                                 const string num = str.substr(0, pos);
517                                 const string den = str.substr(pos + 1);
518                                 global_flags.av1_fps_num = stoi(num);
519                                 global_flags.av1_fps_den = stoi(den);
520                         }
521                         break;
522                 }
523                 case OPTION_AV1_PARAM:
524                         global_flags.av1_extra_param.push_back(optarg);
525                         break;
526                 case OPTION_FLAT_AUDIO:
527                         // If --flat-audio is given, turn off everything that messes with the sound,
528                         // except the final makeup gain.
529                         global_flags.locut_enabled = false;
530                         global_flags.gain_staging_auto = false;
531                         global_flags.compressor_enabled = false;
532                         global_flags.limiter_enabled = false;
533                         break;
534                 case OPTION_GAIN_STAGING:
535                         global_flags.initial_gain_staging_db = atof(optarg);
536                         global_flags.gain_staging_auto = false;
537                         break;
538                 case OPTION_DISABLE_LOCUT:
539                         global_flags.locut_enabled = false;
540                         break;
541                 case OPTION_ENABLE_LOCUT:
542                         global_flags.locut_enabled = true;
543                         break;
544                 case OPTION_DISABLE_GAIN_STAGING_AUTO:
545                         global_flags.gain_staging_auto = false;
546                         break;
547                 case OPTION_ENABLE_GAIN_STAGING_AUTO:
548                         global_flags.gain_staging_auto = true;
549                         break;
550                 case OPTION_DISABLE_COMPRESSOR:
551                         global_flags.compressor_enabled = false;
552                         break;
553                 case OPTION_ENABLE_COMPRESSOR:
554                         global_flags.compressor_enabled = true;
555                         break;
556                 case OPTION_DISABLE_LIMITER:
557                         global_flags.limiter_enabled = false;
558                         break;
559                 case OPTION_ENABLE_LIMITER:
560                         global_flags.limiter_enabled = true;
561                         break;
562                 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
563                         global_flags.final_makeup_gain_auto = false;
564                         break;
565                 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
566                         global_flags.final_makeup_gain_auto = true;
567                         break;
568                 case OPTION_DISABLE_ALSA_OUTPUT:
569                         global_flags.enable_alsa_output = false;
570                         break;
571                 case OPTION_NO_FLUSH_PBOS:
572                         global_flags.flush_pbos = false;
573                         break;
574                 case OPTION_PRINT_VIDEO_LATENCY:
575                         global_flags.print_video_latency = true;
576                         break;
577                 case OPTION_MAX_INPUT_QUEUE_FRAMES:
578                         global_flags.max_input_queue_frames = atoi(optarg);
579                         break;
580                 case OPTION_AUDIO_QUEUE_LENGTH_MS:
581                         global_flags.audio_queue_length_ms = atof(optarg);
582                         break;
583                 case OPTION_OUTPUT_YCBCR_COEFFICIENTS:
584                         output_ycbcr_coefficients = optarg;
585                         break;
586                 case OPTION_OUTPUT_BUFFER_FRAMES:
587                         global_flags.output_buffer_frames = atof(optarg);
588                         break;
589                 case OPTION_OUTPUT_SLOP_FRAMES:
590                         global_flags.output_slop_frames = atof(optarg);
591                         break;
592                 case OPTION_OUTPUT_CARD_UNSYNCHRONIZED:
593                         global_flags.output_card_is_master = false;
594                         break;
595                 case OPTION_TIMECODE_STREAM:
596                         global_flags.display_timecode_in_stream = true;
597                         break;
598                 case OPTION_TIMECODE_STDOUT:
599                         global_flags.display_timecode_on_stdout = true;
600                         break;
601                 case OPTION_QUICK_CUT_KEYS:
602                         global_flags.enable_quick_cut_keys = true;
603                         break;
604                 case OPTION_10_BIT_INPUT:
605                         fprintf(stderr, "NOTE: --10-bit-input is a deprecated alias for --10-bit.\n");
606                         global_flags.bit_depth = 10;
607                         break;
608                 case OPTION_10_BIT_OUTPUT:
609                         fprintf(stderr, "NOTE: --10-bit-output is a deprecated alias for --10-bit.\n");
610                         global_flags.bit_depth = 10;
611                         break;
612                 case OPTION_10_BIT:
613                         global_flags.bit_depth = 10;
614                         break;
615                 case OPTION_INPUT_YCBCR_INTERPRETATION: {
616                         char *ptr = strchr(optarg, ',');
617                         if (ptr == nullptr) {
618                                 fprintf(stderr, "ERROR: Invalid argument '%s' to --input-ycbcr-interpretation (needs a card and an interpretation, separated by comma)\n", optarg);
619                                 exit(1);
620                         }
621                         *ptr = '\0';
622                         const int card_num = atoi(optarg);
623                         if (card_num < 0 || card_num >= MAX_VIDEO_CARDS) {
624                                 fprintf(stderr, "ERROR: Invalid card number %d\n", card_num);
625                                 exit(1);
626                         }
627
628                         YCbCrInterpretation interpretation;
629                         char *interpretation_str = ptr + 1;
630                         ptr = strchr(interpretation_str, ',');
631                         if (ptr != nullptr) {
632                                 *ptr = '\0';
633                                 const char *range = ptr + 1;
634                                 if (strcmp(range, "full") == 0) {
635                                         interpretation.full_range = true;
636                                 } else if (strcmp(range, "limited") == 0) {
637                                         interpretation.full_range = false;
638                                 } else {
639                                         fprintf(stderr, "ERROR: Invalid Y'CbCr range '%s' (must be “full” or “limited”)\n", range);
640                                         exit(1);
641                                 }
642                         }
643
644                         if (strcmp(interpretation_str, "rec601") == 0) {
645                                 interpretation.ycbcr_coefficients_auto = false;
646                                 interpretation.ycbcr_coefficients = movit::YCBCR_REC_601;
647                         } else if (strcmp(interpretation_str, "rec709") == 0) {
648                                 interpretation.ycbcr_coefficients_auto = false;
649                                 interpretation.ycbcr_coefficients = movit::YCBCR_REC_709;
650                         } else if (strcmp(interpretation_str, "auto") == 0) {
651                                 interpretation.ycbcr_coefficients_auto = true;
652                                 if (interpretation.full_range) {
653                                         fprintf(stderr, "ERROR: Cannot use “auto” Y'CbCr coefficients with full range\n");
654                                         exit(1);
655                                 }
656                         } else {
657                                 fprintf(stderr, "ERROR: Invalid Y'CbCr coefficients '%s' (must be “rec601”, “rec709” or “auto”)\n", interpretation_str);
658                                 exit(1);
659                         }
660                         global_flags.ycbcr_interpretation[card_num] = interpretation;
661                         break;
662                 }
663                 case OPTION_FULLSCREEN:
664                         global_flags.fullscreen = true;
665                         break;
666                 case OPTION_MJPEG_EXPORT_CARDS: {
667                         if (card_to_mjpeg_stream_export_set) {
668                                 fprintf(stderr, "ERROR: --mjpeg-export-cards given twice\n");
669                                 exit(1);
670                         }
671                         global_flags.card_to_mjpeg_stream_export = parse_mjpeg_export_cards(optarg);    
672                         card_to_mjpeg_stream_export_set = true;
673                         break;
674                 }
675                 case OPTION_HELP:
676                         usage(program);
677                         exit(0);
678                 default:
679                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
680                         fprintf(stderr, "\n");
681                         usage(program);
682                         exit(1);
683                 }
684         }
685
686         if (global_flags.x264_video_to_http +
687             global_flags.av1_video_to_http > 1) {
688                 fprintf(stderr, "ERROR: --http-{x264,av1}-video are mutually incompatible\n");
689                 exit(1);
690         }
691         if (global_flags.bit_depth == 10) {
692                 global_flags.x264_video_to_disk = true;  // No 10-bit Quick Sync support.
693                 if (!global_flags.av1_video_to_http) {
694                         global_flags.x264_video_to_http = true;
695                 }
696         }
697         if (global_flags.x264_video_to_disk && !global_flags.av1_video_to_http) {
698                 global_flags.x264_video_to_http = true;  // Quick Sync to HTTP but x264 to disk doesn't make sense.
699         }
700         if (global_flags.min_num_cards <= 0) {
701                 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
702                 exit(1);
703         }
704         if (global_flags.max_num_cards <= 0) {
705                 fprintf(stderr, "ERROR: --max-num-cards must be at least 1\n");
706                 exit(1);
707         }
708         if (global_flags.max_num_cards < global_flags.min_num_cards) {
709                 fprintf(stderr, "ERROR: --max-num-cards can not be lower than --num-cards\n");
710                 exit(1);
711         }
712         if (global_flags.output_card < -1 ||
713             global_flags.output_card >= global_flags.max_num_cards) {
714                 fprintf(stderr, "ERROR: --output-card points to a nonexistant card\n");
715                 exit(1);
716         }
717         if (global_flags.enable_audio && !global_flags.transcode_audio && global_flags.stream_audio_codec_name.empty()) {
718                 fprintf(stderr, "ERROR: If not transcoding audio, you must specify ahead-of-time what audio codec is in use\n");
719                 fprintf(stderr, "       (using --http-audio-codec).\n");
720                 exit(1);
721         }
722         if (global_flags.x264_speedcontrol) {
723                 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
724                         fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
725                 }
726                 global_flags.x264_preset = "faster";
727         } else if (global_flags.x264_preset.empty()) {
728                 global_flags.x264_preset = X264_DEFAULT_PRESET;
729         }
730         if (global_flags.x264_separate_disk_preset.empty()) {
731                 global_flags.x264_separate_disk_preset = X264_DEFAULT_PRESET;
732         }
733         if (!theme_dirs.empty()) {
734                 global_flags.theme_dirs = theme_dirs;
735         }
736
737         // In reality, we could probably do with any even value (we subsample
738         // by two in some places), but it's better to be on the safe side
739         // wrt. video codecs and such. (I'd set 16 if I could, but 1080 isn't
740         // divisible by 16.)
741         if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
742             global_flags.height <= 0 || (global_flags.height % 8) != 0) {
743                 fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
744                 exit(1);
745         }
746
747         for (pair<int, int> mapping : global_flags.default_stream_mapping) {
748                 if (mapping.second >= global_flags.max_num_cards) {
749                         fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --max-num-cards)\n",
750                                 mapping.first, mapping.second);
751                         exit(1);
752                 }
753         }
754
755         // Rec. 709 would be the sane thing to do, but it seems many players
756         // just default to BT.601 coefficients no matter what. We _do_ set
757         // the right flags, so that a player that works properly doesn't have
758         // to guess, but it's frequently ignored. See discussions
759         // in e.g. https://trac.ffmpeg.org/ticket/4978; the situation with
760         // browsers is complicated and depends on things like hardware acceleration
761         // (https://bugs.chromium.org/p/chromium/issues/detail?id=333619 for
762         // extensive discussion). VLC generally fixed this as part of 3.0.0
763         // (see e.g. https://github.com/videolan/vlc/commit/bc71288b2e38c07d6921472824b92eef1aa85f7e
764         // and https://github.com/videolan/vlc/commit/c3fc2683a9cde1d42674ebf9935dced05733a215),
765         // but earlier versions were pretty random.
766         //
767         // On the other hand, HDMI/SDI output typically requires Rec. 709 for
768         // HD resolutions (with no way of signaling anything else), which is
769         // a conflicting demand. In this case, we typically let the HDMI/SDI
770         // output win if it is active, but the user can override this.
771         if (output_ycbcr_coefficients == "auto") {
772                 // Essentially: BT.709 if HDMI/SDI output is on, otherwise BT.601.
773                 global_flags.ycbcr_rec709_coefficients = false;
774                 global_flags.ycbcr_auto_coefficients = true;
775         } else if (output_ycbcr_coefficients == "rec709") {
776                 global_flags.ycbcr_rec709_coefficients = true;
777                 global_flags.ycbcr_auto_coefficients = false;
778         } else if (output_ycbcr_coefficients == "rec601") {
779                 global_flags.ycbcr_rec709_coefficients = false;
780                 global_flags.ycbcr_auto_coefficients = false;
781         } else {
782                 fprintf(stderr, "ERROR: --output-ycbcr-coefficients must be “rec601”, “rec709” or “auto”\n");
783                 exit(1);
784         }
785
786         if (global_flags.output_buffer_frames < 0.0f) {
787                 // Actually, even zero probably won't make sense; there is some internal
788                 // delay to the card.
789                 fprintf(stderr, "ERROR: --output-buffer-frames can't be negative.\n");
790                 exit(1);
791         }
792         if (global_flags.output_slop_frames < 0.0f) {
793                 fprintf(stderr, "ERROR: --output-slop-frames can't be negative.\n");
794                 exit(1);
795         }
796         if (global_flags.max_input_queue_frames < 1) {
797                 fprintf(stderr, "ERROR: --max-input-queue-frames must be at least 1.\n");
798                 exit(1);
799         }
800         if (global_flags.max_input_queue_frames > 10) {
801                 fprintf(stderr, "WARNING: --max-input-queue-frames has little effect over 10.\n");
802         }
803
804         if (!isinf(global_flags.x264_crf)) {  // CRF mode is selected.
805                 if (global_flags.x264_bitrate != -1) {
806                         fprintf(stderr, "ERROR: --x264-bitrate and --x264-crf are mutually incompatible.\n");
807                         exit(1);
808                 }
809         } else if (global_flags.x264_bitrate == -1) {
810                 global_flags.x264_bitrate = DEFAULT_X264_OUTPUT_BIT_RATE;
811         }
812
813         if (!isinf(global_flags.x264_separate_disk_crf)) {  // CRF mode is selected.
814                 if (global_flags.x264_separate_disk_bitrate != -1) {
815                         fprintf(stderr, "ERROR: --x264-separate-disk-bitrate and --x264-separate-disk-crf are mutually incompatible.\n");
816                         exit(1);
817                 }
818         } else if (global_flags.x264_separate_disk_bitrate == -1) {
819                 global_flags.x264_separate_disk_bitrate = DEFAULT_X264_OUTPUT_BIT_RATE;
820         }
821
822         if (!card_to_mjpeg_stream_export_set) {
823                 // Fill in the default mapping (export all cards, in order).
824                 for (unsigned card_idx = 0; card_idx < unsigned(global_flags.max_num_cards); ++card_idx) {
825                         global_flags.card_to_mjpeg_stream_export[card_idx] = card_idx;
826                 }
827         }
828 }