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