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