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