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