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