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