14 // Long options that have no corresponding short option.
19 OPTION_FAKE_CARDS_AUDIO,
20 OPTION_HTTP_UNCOMPRESSED_VIDEO,
21 OPTION_HTTP_X264_VIDEO,
24 OPTION_X264_SPEEDCONTROL,
25 OPTION_X264_SPEEDCONTROL_VERBOSE,
27 OPTION_X264_VBV_BUFSIZE,
28 OPTION_X264_VBV_MAX_BITRATE,
31 OPTION_HTTP_COARSE_TIMEBASE,
32 OPTION_HTTP_AUDIO_CODEC,
33 OPTION_HTTP_AUDIO_BITRATE,
38 OPTION_DISABLE_GAIN_STAGING_AUTO,
39 OPTION_ENABLE_GAIN_STAGING_AUTO,
40 OPTION_DISABLE_COMPRESSOR,
41 OPTION_ENABLE_COMPRESSOR,
42 OPTION_DISABLE_LIMITER,
43 OPTION_ENABLE_LIMITER,
44 OPTION_DISABLE_MAKEUP_GAIN_AUTO,
45 OPTION_ENABLE_MAKEUP_GAIN_AUTO,
46 OPTION_DISABLE_ALSA_OUTPUT,
48 OPTION_PRINT_VIDEO_LATENCY,
49 OPTION_AUDIO_QUEUE_LENGTH_MS,
50 OPTION_OUTPUT_YCBCR_COEFFICIENTS,
57 fprintf(stderr, "Usage: nageru [OPTION]...\n");
58 fprintf(stderr, "\n");
59 fprintf(stderr, " --help print usage information\n");
60 fprintf(stderr, " -w, --width output width in pixels (default 1280)\n");
61 fprintf(stderr, " -h, --height output height in pixels (default 720)\n");
62 fprintf(stderr, " -c, --num-cards set number of input cards (default 2)\n");
63 fprintf(stderr, " -o, --output-card=CARD also output signal to the given card (default none)\n");
64 fprintf(stderr, " -t, --theme=FILE choose theme (default theme.lua)\n");
65 fprintf(stderr, " -I, --theme-dir=DIR search for theme in this directory (can be given multiple times)\n");
66 fprintf(stderr, " -v, --va-display=SPEC VA-API device for H.264 encoding\n");
67 fprintf(stderr, " ($DISPLAY spec or /dev/dri/render* path)\n");
68 fprintf(stderr, " -m, --map-signal=SIGNAL,CARD set a default card mapping (can be given multiple times)\n");
69 fprintf(stderr, " -M, --input-mapping=FILE start with the given audio input mapping (implies --multichannel)\n");
70 fprintf(stderr, " --multichannel start in multichannel audio mapping mode\n");
71 fprintf(stderr, " --midi-mapping=FILE start with the given MIDI controller mapping (implies --multichannel)\n");
72 fprintf(stderr, " --fake-cards-audio make fake (disconnected) cards output a simple tone\n");
73 fprintf(stderr, " --http-uncompressed-video send uncompressed NV12 video to HTTP clients\n");
74 fprintf(stderr, " --http-x264-video send x264-compressed video to HTTP clients\n");
75 fprintf(stderr, " --x264-preset x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
76 fprintf(stderr, " --x264-tune x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
77 fprintf(stderr, " --x264-speedcontrol try to match x264 preset to available CPU speed\n");
78 fprintf(stderr, " --x264-speedcontrol-verbose output speedcontrol debugging statistics\n");
79 fprintf(stderr, " --x264-bitrate x264 bitrate (in kilobit/sec, default %d)\n",
80 DEFAULT_X264_OUTPUT_BIT_RATE);
81 fprintf(stderr, " --x264-vbv-bufsize x264 VBV size (in kilobits, 0 = one-frame VBV,\n");
82 fprintf(stderr, " default: same as --x264-bitrate, that is, one-second VBV)\n");
83 fprintf(stderr, " --x264-vbv-max-bitrate x264 local max bitrate (in kilobit/sec per --vbv-bufsize,\n");
84 fprintf(stderr, " 0 = no limit, default: same as --x264-bitrate, i.e., CBR)\n");
85 fprintf(stderr, " --x264-param=NAME[,VALUE] set any x264 parameter, for fine tuning\n");
86 fprintf(stderr, " --http-mux=NAME mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
87 fprintf(stderr, " --http-audio-codec=NAME audio codec to use for HTTP streams\n");
88 fprintf(stderr, " (default is to use the same as for the recording)\n");
89 fprintf(stderr, " --http-audio-bitrate=KBITS audio codec bit rate to use for HTTP streams\n");
90 fprintf(stderr, " (default is %d, ignored unless --http-audio-codec is set)\n",
91 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
92 fprintf(stderr, " --http-coarse-timebase use less timebase for HTTP (recommended for muxers\n");
93 fprintf(stderr, " that handle large pts poorly, like e.g. MP4)\n");
94 fprintf(stderr, " --flat-audio start with most audio processing turned off\n");
95 fprintf(stderr, " (can be overridden by e.g. --enable-limiter)\n");
96 fprintf(stderr, " --gain-staging=DB set initial gain staging to the given value\n");
97 fprintf(stderr, " (--disable-gain-staging-auto)\n");
98 fprintf(stderr, " --disable-locut turn off locut filter (also --enable)\n");
99 fprintf(stderr, " --disable-gain-staging-auto turn off automatic gain staging (also --enable)\n");
100 fprintf(stderr, " --disable-compressor turn off regular compressor (also --enable)\n");
101 fprintf(stderr, " --disable-limiter turn off limiter (also --enable)\n");
102 fprintf(stderr, " --disable-makeup-gain-auto turn off auto-adjustment of final makeup gain (also --enable)\n");
103 fprintf(stderr, " --disable-alsa-output disable audio monitoring via ALSA\n");
104 fprintf(stderr, " --no-flush-pbos do not explicitly signal texture data uploads\n");
105 fprintf(stderr, " (will give display corruption, but makes it\n");
106 fprintf(stderr, " possible to run with apitrace in real time)\n");
107 fprintf(stderr, " --print-video-latency print out measurements of video latency on stdout\n");
108 fprintf(stderr, " --audio-queue-length-ms=MS length of audio resampling queue (default 100.0)\n");
109 fprintf(stderr, " --output-ycbcr-coefficients={rec601,rec709,auto}\n");
110 fprintf(stderr, " Y'CbCr coefficient standard of output (default auto)\n");
111 fprintf(stderr, " auto is rec709 if and only if --output-card is used\n");
112 fprintf(stderr, " and a HD resolution is set\n");
113 fprintf(stderr, " --output-buffer-frames=NUM number of frames in output buffer for --output-card,\n");
114 fprintf(stderr, " can be fractional (default 6.0); note also\n");
115 fprintf(stderr, " the audio queue can't be much longer than this\n");
116 fprintf(stderr, " --output-slop-frames=NUM if more less than this number of frames behind for\n");
117 fprintf(stderr, " --output-card, try to submit anyway instead of\n");
118 fprintf(stderr, " dropping the frame (default 0.5)\n");
121 void parse_flags(int argc, char * const argv[])
123 static const option long_options[] = {
124 { "help", no_argument, 0, OPTION_HELP },
125 { "width", required_argument, 0, 'w' },
126 { "height", required_argument, 0, 'h' },
127 { "num-cards", required_argument, 0, 'c' },
128 { "output-card", required_argument, 0, 'o' },
129 { "theme", required_argument, 0, 't' },
130 { "theme-dir", required_argument, 0, 'I' },
131 { "map-signal", required_argument, 0, 'm' },
132 { "input-mapping", required_argument, 0, 'M' },
133 { "va-display", required_argument, 0, 'v' },
134 { "multichannel", no_argument, 0, OPTION_MULTICHANNEL },
135 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
136 { "fake-cards-audio", no_argument, 0, OPTION_FAKE_CARDS_AUDIO },
137 { "http-uncompressed-video", no_argument, 0, OPTION_HTTP_UNCOMPRESSED_VIDEO },
138 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
139 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
140 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
141 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
142 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
143 { "x264-bitrate", required_argument, 0, OPTION_X264_BITRATE },
144 { "x264-vbv-bufsize", required_argument, 0, OPTION_X264_VBV_BUFSIZE },
145 { "x264-vbv-max-bitrate", required_argument, 0, OPTION_X264_VBV_MAX_BITRATE },
146 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
147 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
148 { "http-coarse-timebase", no_argument, 0, OPTION_HTTP_COARSE_TIMEBASE },
149 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
150 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
151 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
152 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
153 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
154 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
155 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
156 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
157 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
158 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
159 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
160 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
161 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
162 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
163 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
164 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
165 { "print-video-latency", no_argument, 0, OPTION_PRINT_VIDEO_LATENCY },
166 { "audio-queue-length-ms", required_argument, 0, OPTION_AUDIO_QUEUE_LENGTH_MS },
167 { "output-ycbcr-coefficients", required_argument, 0, OPTION_OUTPUT_YCBCR_COEFFICIENTS },
168 { "output-buffer-frames", required_argument, 0, OUTPUT_BUFFER_FRAMES },
169 { "output-slop-frames", required_argument, 0, OUTPUT_SLOP_FRAMES },
172 vector<string> theme_dirs;
173 string output_ycbcr_coefficients = "auto";
175 int option_index = 0;
176 int c = getopt_long(argc, argv, "c:t:I:v:m:M:w:h:", long_options, &option_index);
183 global_flags.width = atoi(optarg);
186 global_flags.height = atoi(optarg);
189 global_flags.num_cards = atoi(optarg);
192 global_flags.output_card = atoi(optarg);
195 global_flags.theme_filename = optarg;
198 theme_dirs.push_back(optarg);
201 char *ptr = strchr(optarg, ',');
202 if (ptr == nullptr) {
203 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
207 const int signal_num = atoi(optarg);
208 const int card_num = atoi(ptr + 1);
209 if (global_flags.default_stream_mapping.count(signal_num)) {
210 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
211 signal_num, global_flags.default_stream_mapping[signal_num]);
214 global_flags.default_stream_mapping[signal_num] = card_num;
218 global_flags.input_mapping_filename = optarg;
220 case OPTION_MULTICHANNEL:
221 global_flags.multichannel_mapping_mode = true;
224 global_flags.va_display = optarg;
226 case OPTION_MIDI_MAPPING:
227 global_flags.midi_mapping_filename = optarg;
228 global_flags.multichannel_mapping_mode = true;
230 case OPTION_FAKE_CARDS_AUDIO:
231 global_flags.fake_cards_audio = true;
233 case OPTION_HTTP_UNCOMPRESSED_VIDEO:
234 global_flags.uncompressed_video_to_http = true;
236 case OPTION_HTTP_MUX:
237 global_flags.stream_mux_name = optarg;
239 case OPTION_HTTP_COARSE_TIMEBASE:
240 global_flags.stream_coarse_timebase = true;
242 case OPTION_HTTP_AUDIO_CODEC:
243 global_flags.stream_audio_codec_name = optarg;
245 case OPTION_HTTP_AUDIO_BITRATE:
246 global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
248 case OPTION_HTTP_X264_VIDEO:
249 global_flags.x264_video_to_http = true;
251 case OPTION_X264_PRESET:
252 global_flags.x264_preset = optarg;
254 case OPTION_X264_TUNE:
255 global_flags.x264_tune = optarg;
257 case OPTION_X264_SPEEDCONTROL:
258 global_flags.x264_speedcontrol = true;
260 case OPTION_X264_SPEEDCONTROL_VERBOSE:
261 global_flags.x264_speedcontrol_verbose = true;
263 case OPTION_X264_BITRATE:
264 global_flags.x264_bitrate = atoi(optarg);
266 case OPTION_X264_VBV_BUFSIZE:
267 global_flags.x264_vbv_buffer_size = atoi(optarg);
269 case OPTION_X264_VBV_MAX_BITRATE:
270 global_flags.x264_vbv_max_bitrate = atoi(optarg);
272 case OPTION_X264_PARAM:
273 global_flags.x264_extra_param.push_back(optarg);
275 case OPTION_FLAT_AUDIO:
276 // If --flat-audio is given, turn off everything that messes with the sound,
277 // except the final makeup gain.
278 global_flags.locut_enabled = false;
279 global_flags.gain_staging_auto = false;
280 global_flags.compressor_enabled = false;
281 global_flags.limiter_enabled = false;
283 case OPTION_GAIN_STAGING:
284 global_flags.initial_gain_staging_db = atof(optarg);
285 global_flags.gain_staging_auto = false;
287 case OPTION_DISABLE_LOCUT:
288 global_flags.locut_enabled = false;
290 case OPTION_ENABLE_LOCUT:
291 global_flags.locut_enabled = true;
293 case OPTION_DISABLE_GAIN_STAGING_AUTO:
294 global_flags.gain_staging_auto = false;
296 case OPTION_ENABLE_GAIN_STAGING_AUTO:
297 global_flags.gain_staging_auto = true;
299 case OPTION_DISABLE_COMPRESSOR:
300 global_flags.compressor_enabled = false;
302 case OPTION_ENABLE_COMPRESSOR:
303 global_flags.compressor_enabled = true;
305 case OPTION_DISABLE_LIMITER:
306 global_flags.limiter_enabled = false;
308 case OPTION_ENABLE_LIMITER:
309 global_flags.limiter_enabled = true;
311 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
312 global_flags.final_makeup_gain_auto = false;
314 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
315 global_flags.final_makeup_gain_auto = true;
317 case OPTION_DISABLE_ALSA_OUTPUT:
318 global_flags.enable_alsa_output = false;
320 case OPTION_NO_FLUSH_PBOS:
321 global_flags.flush_pbos = false;
323 case OPTION_PRINT_VIDEO_LATENCY:
324 global_flags.print_video_latency = true;
326 case OPTION_AUDIO_QUEUE_LENGTH_MS:
327 global_flags.audio_queue_length_ms = atof(optarg);
329 case OPTION_OUTPUT_YCBCR_COEFFICIENTS:
330 output_ycbcr_coefficients = optarg;
332 case OUTPUT_BUFFER_FRAMES:
333 global_flags.output_buffer_frames = atof(optarg);
335 case OUTPUT_SLOP_FRAMES:
336 global_flags.output_slop_frames = atof(optarg);
342 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
343 fprintf(stderr, "\n");
349 if (global_flags.uncompressed_video_to_http &&
350 global_flags.x264_video_to_http) {
351 fprintf(stderr, "ERROR: --http-uncompressed-video and --http-x264-video are mutually incompatible\n");
354 if (global_flags.num_cards <= 0) {
355 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
358 if (global_flags.output_card < -1 ||
359 global_flags.output_card >= global_flags.num_cards) {
360 fprintf(stderr, "ERROR: --output-card points to a nonexistant card\n");
363 if (global_flags.x264_speedcontrol) {
364 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
365 fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
367 global_flags.x264_preset = "faster";
368 } else if (global_flags.x264_preset.empty()) {
369 global_flags.x264_preset = X264_DEFAULT_PRESET;
371 if (!theme_dirs.empty()) {
372 global_flags.theme_dirs = theme_dirs;
375 // In reality, we could probably do with any even value (we subsample
376 // by two in some places), but it's better to be on the safe side
377 // wrt. video codecs and such. (I'd set 16 if I could, but 1080 isn't
379 if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
380 global_flags.height <= 0 || (global_flags.height % 8) != 0) {
381 fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
385 for (pair<int, int> mapping : global_flags.default_stream_mapping) {
386 if (mapping.second >= global_flags.num_cards) {
387 fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --num-cards)\n",
388 mapping.first, mapping.second);
393 // Rec. 709 would be the sane thing to do, but it seems many players
394 // just default to BT.601 coefficients no matter what. We _do_ set
395 // the right flags, so that a player that works properly doesn't have
396 // to guess, but it's frequently ignored. See discussions
397 // in e.g. https://trac.ffmpeg.org/ticket/4978; the situation with
398 // browsers is complicated and depends on things like hardware acceleration
399 // (https://bugs.chromium.org/p/chromium/issues/detail?id=333619 for
400 // extensive discussion). VLC generally fixed this as part of 3.0.0
401 // (see e.g. https://github.com/videolan/vlc/commit/bc71288b2e38c07d6921472824b92eef1aa85f7e
402 // and https://github.com/videolan/vlc/commit/c3fc2683a9cde1d42674ebf9935dced05733a215),
403 // but earlier versions were pretty random.
405 // On the other hand, HDMI/SDI output typically requires Rec. 709 for
406 // HD resolutions (with no way of signaling anything else), which is
407 // a conflicting demand. In this case, we typically let the HDMI/SDI
408 // output win, but the user can override this.
409 if (output_ycbcr_coefficients == "auto") {
410 if (global_flags.output_card >= 0 && global_flags.width >= 1280) {
411 global_flags.ycbcr_rec709_coefficients = true;
413 global_flags.ycbcr_rec709_coefficients = false;
415 } else if (output_ycbcr_coefficients == "rec709") {
416 global_flags.ycbcr_rec709_coefficients = true;
417 } else if (output_ycbcr_coefficients == "rec601") {
418 global_flags.ycbcr_rec709_coefficients = false;
420 fprintf(stderr, "ERROR: --output-ycbcr-coefficients must be “rec601”, “rec709” or “auto”\n");
424 if (global_flags.output_buffer_frames < 0.0f) {
425 // Actually, even zero probably won't make sense; there is some internal
426 // delay to the card.
427 fprintf(stderr, "ERROR: --output-buffer-frames can't be negative.\n");
430 if (global_flags.output_slop_frames < 0.0f) {
431 fprintf(stderr, "ERROR: --output-slop-frames can't be negative.\n");