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