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