14 // Long options that have no corresponding short option.
21 OPTION_DEFAULT_HDMI_INPUT,
22 OPTION_FAKE_CARDS_AUDIO,
24 OPTION_HTTP_UNCOMPRESSED_VIDEO,
25 OPTION_HTTP_X264_VIDEO,
26 OPTION_HTTP_AV1_VIDEO,
27 OPTION_RECORD_X264_VIDEO,
28 OPTION_SEPARATE_X264_DISK_ENCODE,
31 OPTION_X264_SPEEDCONTROL,
32 OPTION_X264_SPEEDCONTROL_VERBOSE,
35 OPTION_X264_VBV_BUFSIZE,
36 OPTION_X264_VBV_MAX_BITRATE,
38 OPTION_X264_SEPARATE_DISK_PRESET,
39 OPTION_X264_SEPARATE_DISK_TUNE,
40 OPTION_X264_SEPARATE_DISK_BITRATE,
41 OPTION_X264_SEPARATE_DISK_CRF,
42 OPTION_X264_SEPARATE_DISK_PARAM,
48 OPTION_HTTP_COARSE_TIMEBASE,
49 OPTION_HTTP_AUDIO_CODEC,
50 OPTION_HTTP_AUDIO_BITRATE,
54 OPTION_SRT_DESTINATION,
56 OPTION_SRT_PASSPHRASE,
57 OPTION_SRT_YOUTUBE_STREAM_KEY,
59 OPTION_NO_TRANSCODE_VIDEO,
60 OPTION_NO_TRANSCODE_AUDIO,
66 OPTION_DISABLE_GAIN_STAGING_AUTO,
67 OPTION_ENABLE_GAIN_STAGING_AUTO,
68 OPTION_DISABLE_COMPRESSOR,
69 OPTION_ENABLE_COMPRESSOR,
70 OPTION_DISABLE_LIMITER,
71 OPTION_ENABLE_LIMITER,
72 OPTION_DISABLE_MAKEUP_GAIN_AUTO,
73 OPTION_ENABLE_MAKEUP_GAIN_AUTO,
74 OPTION_DISABLE_ALSA_OUTPUT,
76 OPTION_PRINT_VIDEO_LATENCY,
77 OPTION_MAX_INPUT_QUEUE_FRAMES,
78 OPTION_AUDIO_QUEUE_LENGTH_MS,
79 OPTION_OUTPUT_YCBCR_COEFFICIENTS,
80 OPTION_OUTPUT_BUFFER_FRAMES,
81 OPTION_OUTPUT_SLOP_FRAMES,
82 OPTION_OUTPUT_CARD_UNSYNCHRONIZED,
83 OPTION_TIMECODE_STREAM,
84 OPTION_TIMECODE_STDOUT,
85 OPTION_QUICK_CUT_KEYS,
89 OPTION_INPUT_YCBCR_INTERPRETATION,
90 OPTION_MJPEG_EXPORT_CARDS,
93 map<unsigned, unsigned> parse_mjpeg_export_cards(char *optarg)
95 map<unsigned, unsigned> ret;
96 if (optarg[0] == '\0') {
100 unsigned stream_idx = 0;
101 char *start = optarg;
103 char *end = strchr(start, ',');
104 if (end != nullptr) {
108 unsigned range_begin, range_end;
109 if (sscanf(start, "%u-%u", &range_begin, &range_end) != 2) {
110 range_begin = range_end = atoi(start);
112 if (range_end < range_begin) {
113 fprintf(stderr, "ERROR: Invalid range %u-%u in --mjpeg-export-cards=\n", range_begin, range_end);
116 if (range_end >= unsigned(global_flags.max_num_cards)) {
117 // There are situations where we could possibly want to
118 // include FFmpeg inputs (CEF inputs are unlikely),
119 // but they're not necessarily in 4:2:2 Y'CbCr, so it would
120 // require more functionality the the JPEG encoder.
121 fprintf(stderr, "ERROR: Asked for (zero-indexed) card %u in --mjpeg-export-cards=, but there are only %u cards\n",
122 range_end, global_flags.max_num_cards);
125 for (unsigned card_idx = range_begin; card_idx <= range_end; ++card_idx) {
126 if (ret.count(card_idx)) {
127 fprintf(stderr, "ERROR: Card %u was given twice in --mjpeg-export-cards=\n", card_idx);
130 ret[card_idx] = stream_idx++;
132 if (end == nullptr) {
141 void usage(Program program)
143 if (program == PROGRAM_KAERU) {
144 fprintf(stderr, "Usage: kaeru [OPTION]... SOURCE_URL\n");
146 fprintf(stderr, "Usage: nageru [OPTION]...\n");
148 fprintf(stderr, "\n");
149 fprintf(stderr, " --help print usage information\n");
150 if (program == PROGRAM_NAGERU) {
151 fprintf(stderr, " --fullscreen run in full screen, with no decorations\n");
153 fprintf(stderr, " -w, --width output width in pixels (default 1280)\n");
154 fprintf(stderr, " -h, --height output height in pixels (default 720)\n");
155 if (program == PROGRAM_NAGERU) {
156 fprintf(stderr, " -c, --num-cards set minimum number of input cards (default 2)\n");
157 fprintf(stderr, " --max-num-cards set maximum number of input cards (default %d)\n", MAX_VIDEO_CARDS);
158 fprintf(stderr, " -o, --output-card=CARD also output signal to the given card (default none)\n");
159 fprintf(stderr, " -t, --theme=FILE choose theme (default theme.lua)\n");
160 fprintf(stderr, " -I, --theme-dir=DIR search for theme in this directory (can be given multiple times)\n");
161 fprintf(stderr, " -r, --recording-dir=DIR where to store disk recording\n");
162 fprintf(stderr, " -v, --va-display=SPEC VA-API device for H.264 encoding\n");
163 fprintf(stderr, " ($DISPLAY spec or /dev/dri/render* path)\n");
164 fprintf(stderr, " -m, --map-signal=SIGNAL,CARD set a default card mapping (can be given multiple times)\n");
165 fprintf(stderr, " -M, --input-mapping=FILE start with the given audio input mapping (implies --multichannel)\n");
166 fprintf(stderr, " --multichannel start in multichannel audio mapping mode\n");
167 fprintf(stderr, " --midi-mapping=FILE start with the given MIDI controller mapping (implies --multichannel)\n");
168 fprintf(stderr, " --default-hdmi-input default to HDMI over SDI inputs for cards that have both\n");
169 fprintf(stderr, " --fake-cards-audio make fake (disconnected) cards output a simple tone\n");
170 fprintf(stderr, " --v4l-output=DEVICE send video (no audio) to V4L2 output/loopback\n");
171 fprintf(stderr, " --http-x264-video send x264-compressed video to HTTP clients\n");
172 fprintf(stderr, " --record-x264-video store x264-compressed video to disk (implies --http-x264-video,\n");
173 fprintf(stderr, " removes the need for working VA-API encoding)\n");
174 fprintf(stderr, " --separate-x264-disk-encode run a different x264 encoder for disk recording\n");
175 fprintf(stderr, " (implies --record-x264-video)\n");
177 fprintf(stderr, " --x264-preset x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
178 fprintf(stderr, " --x264-tune x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
179 fprintf(stderr, " --x264-speedcontrol try to match x264 preset to available CPU speed\n");
180 fprintf(stderr, " --x264-bitrate x264 bitrate (in kilobit/sec, default %d)\n",
181 DEFAULT_X264_OUTPUT_BIT_RATE);
182 fprintf(stderr, " --x264-crf=VALUE quality-based VBR (-12 to 51), incompatible with --x264-bitrate\n");
183 fprintf(stderr, " --x264-param=NAME[,VALUE] set any x264 parameter, for fine tuning\n");
184 if (program == PROGRAM_NAGERU) {
185 fprintf(stderr, " --x264-separate-disk-preset x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
186 fprintf(stderr, " --x264-separate-disk-tune x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
187 fprintf(stderr, " --x264-separate-disk-bitrate x264 bitrate (in kilobit/sec, default %d)\n",
188 DEFAULT_X264_OUTPUT_BIT_RATE);
189 fprintf(stderr, " --x264-separate-disk-crf=VALUE quality-based VBR (-12 to 51), \n");
190 fprintf(stderr, " incompatible with --x264-separate-disk-bitrate\n");
191 fprintf(stderr, " --x264-separate-disk-param=NAME[,VALUE] set any x264 parameter, for fine tuning\n");
194 fprintf(stderr, " --http-av1-video send AV1-compressed video to HTTP clients and SRT output\n");
195 fprintf(stderr, " --av1-preset SVT-AV1 quality preset (default %d, from -2 to 13)\n",
197 fprintf(stderr, " --av1-bitrate AV1 bitrate (in kilobit/sec, default %d)\n",
198 DEFAULT_AV1_OUTPUT_BIT_RATE);
199 fprintf(stderr, " --av1-fps=NUM[/DEN] AV1 encoded frame rate (default %d/%d)\n",
200 DEFAULT_AV1_FPS_NUM, DEFAULT_AV1_FPS_DEN);
201 fprintf(stderr, " --av1-param=NAME[,VALUE] set any SVT-AV1 parameter, for fine tuning\n");
203 fprintf(stderr, " --http-mux=NAME mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
204 fprintf(stderr, " --http-audio-codec=NAME audio codec to use for HTTP streams and SRT output\n");
205 fprintf(stderr, " (default is to use the same as for the recording)\n");
206 fprintf(stderr, " --http-audio-bitrate=KBITS audio codec bit rate to use for HTTP streams and SRT output\n");
207 fprintf(stderr, " (default is %d, ignored unless --http-audio-codec is set)\n",
208 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
209 fprintf(stderr, " --http-port=PORT which port to use for the built-in HTTP server\n");
210 fprintf(stderr, " (default is %d)\n", DEFAULT_HTTPD_PORT);
212 fprintf(stderr, " --srt-port=PORT which port to use for receiving SRT streams\n");
213 fprintf(stderr, " (default is %d)\n", DEFAULT_SRT_PORT);
214 fprintf(stderr, " --no-srt disable receiving SRT streams\n");
216 if (program == PROGRAM_KAERU) {
217 fprintf(stderr, " --no-transcode-video copy encoded video raw from the source stream\n");
218 fprintf(stderr, " (experimental, must be H.264)\n");
219 fprintf(stderr, " --no-transcode-audio copy encoded audio raw from the source stream\n");
220 fprintf(stderr, " (requires --http-audio-codec= to be set)\n");
221 fprintf(stderr, " --disable-audio do not include any audio in the stream\n");
223 if (program == PROGRAM_NAGERU) {
225 fprintf(stderr, " --srt-destination=HOST:PORT send HTTP video stream also to given destination\n");
226 fprintf(stderr, " --srt-streamid=STREAMID SRT stream identifying ID\n");
227 fprintf(stderr, " --srt-passphrase=PASSPHRASE SRT encryption key\n");
228 fprintf(stderr, " --srt-youtube-stream-key=KEY shortcut for --srt-destination=a.srt.youtube.com:2010\n");
229 fprintf(stderr, " --srt-streamid=#!::u=<KEY>,copy=0,encoder=Nageru\n");
230 fprintf(stderr, " --srt-latency=MS output SRT latency in milliseconds (default is %d)\n", DEFAULT_SRT_OUTPUT_LATENCY_MS);
232 fprintf(stderr, " --flat-audio start with most audio processing turned off\n");
233 fprintf(stderr, " (can be overridden by e.g. --enable-limiter)\n");
234 fprintf(stderr, " --gain-staging=DB set initial gain staging to the given value\n");
235 fprintf(stderr, " (--disable-gain-staging-auto)\n");
236 fprintf(stderr, " --disable-locut turn off locut filter (also --enable)\n");
237 fprintf(stderr, " --disable-gain-staging-auto turn off automatic gain staging (also --enable)\n");
238 fprintf(stderr, " --disable-compressor turn off regular compressor (also --enable)\n");
239 fprintf(stderr, " --disable-limiter turn off limiter (also --enable)\n");
240 fprintf(stderr, " --disable-makeup-gain-auto turn off auto-adjustment of final makeup gain (also --enable)\n");
241 fprintf(stderr, " --disable-alsa-output disable audio monitoring via ALSA\n");
242 fprintf(stderr, " --print-video-latency print out measurements of video latency on stdout\n");
243 fprintf(stderr, " --max-input-queue-frames=FRAMES never keep more than FRAMES frames for each card\n");
244 fprintf(stderr, " (default 6, minimum 1)\n");
245 fprintf(stderr, " --audio-queue-length-ms=MS length of audio resampling queue (default 100.0)\n");
246 fprintf(stderr, " --output-ycbcr-coefficients={rec601,rec709,auto}\n");
247 fprintf(stderr, " Y'CbCr coefficient standard of output (default auto)\n");
248 fprintf(stderr, " auto is rec601, unless --output-card is used\n");
249 fprintf(stderr, " and a Rec. 709 mode (typically HD modes) is in use\n");
250 fprintf(stderr, " --output-buffer-frames=NUM number of frames in output buffer for --output-card,\n");
251 fprintf(stderr, " can be fractional (default 6.0); note also\n");
252 fprintf(stderr, " the audio queue can't be much longer than this\n");
253 fprintf(stderr, " --output-slop-frames=NUM if more less than this number of frames behind for\n");
254 fprintf(stderr, " --output-card, try to submit anyway instead of\n");
255 fprintf(stderr, " dropping the frame (default 0.5)\n");
256 fprintf(stderr, " --output-card-unsynchronized if --output-card is in use, do _not_ use it as\n");
257 fprintf(stderr, " master clock (may give jittery output and audio breakups)\n");
258 fprintf(stderr, " --timecode-stream show timestamp and timecode in stream\n");
259 fprintf(stderr, " --timecode-stdout show timestamp and timecode on standard output\n");
260 fprintf(stderr, " --quick-cut-keys enable direct cutting by Q, W, E, ... keys\n");
261 fprintf(stderr, " --10-bit use 10-bit color depth\n");
262 fprintf(stderr, " --input-ycbcr-interpretation=CARD,{rec601,rec709,auto}[,{limited,full}]\n");
263 fprintf(stderr, " Y'CbCr coefficient standard of card CARD (default auto)\n");
264 fprintf(stderr, " auto is rec601 for SD, rec709 for HD, always limited\n");
265 fprintf(stderr, " limited means standard 0-240/0-235 input range (for 8-bit)\n");
266 fprintf(stderr, " --mjpeg-export-cards=RANGE[,RANGE...]\n");
267 fprintf(stderr, " export the given cards in MJPEG format to /multicam.mp4,\n");
268 fprintf(stderr, " in the given order (ranges can be either single card indexes\n");
269 fprintf(stderr, " or pairs like 1-3 for camera 1,2,3; default is all cards)\n");
273 void parse_flags(Program program, int argc, char * const argv[])
275 static const option long_options[] = {
276 { "help", no_argument, 0, OPTION_HELP },
277 { "fullscreen", no_argument, 0, OPTION_FULLSCREEN },
278 { "width", required_argument, 0, 'w' },
279 { "height", required_argument, 0, 'h' },
280 { "num-cards", required_argument, 0, 'c' },
281 { "max-num-cards", required_argument, 0, OPTION_MAX_NUM_CARDS },
282 { "output-card", required_argument, 0, 'o' },
283 { "theme", required_argument, 0, 't' },
284 { "theme-dir", required_argument, 0, 'I' },
285 { "recording-dir", required_argument, 0, 'r' },
286 { "map-signal", required_argument, 0, 'm' },
287 { "input-mapping", required_argument, 0, 'M' },
288 { "va-display", required_argument, 0, 'v' },
289 { "multichannel", no_argument, 0, OPTION_MULTICHANNEL },
290 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
291 { "default-hdmi-input", no_argument, 0, OPTION_DEFAULT_HDMI_INPUT },
292 { "fake-cards-audio", no_argument, 0, OPTION_FAKE_CARDS_AUDIO },
293 { "v4l-output", required_argument, 0, OPTION_V4L_OUTPUT },
294 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
295 { "record-x264-video", no_argument, 0, OPTION_RECORD_X264_VIDEO },
296 { "separate-x264-disk-encode", no_argument, 0, OPTION_SEPARATE_X264_DISK_ENCODE },
297 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
298 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
299 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
300 { "x264-crf", required_argument, 0, OPTION_X264_CRF },
301 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
302 { "x264-separate-disk-preset", required_argument, 0, OPTION_X264_SEPARATE_DISK_PRESET },
303 { "x264-separate-disk-tune", required_argument, 0, OPTION_X264_SEPARATE_DISK_TUNE },
304 { "x264-separate-disk-bitrate", required_argument, 0, OPTION_X264_SEPARATE_DISK_BITRATE },
305 { "x264-separate-disk-crf", required_argument, 0, OPTION_X264_SEPARATE_DISK_CRF },
306 { "x264-separate-disk-param", required_argument, 0, OPTION_X264_SEPARATE_DISK_PARAM },
308 { "http-av1-video", no_argument, 0, OPTION_HTTP_AV1_VIDEO },
309 { "av1-preset", required_argument, 0, OPTION_AV1_PRESET },
310 { "av1-bitrate", required_argument, 0, OPTION_AV1_BITRATE },
311 { "av1-fps", required_argument, 0, OPTION_AV1_FPS },
312 { "av1-param", required_argument, 0, OPTION_AV1_PARAM },
314 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
315 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
316 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
317 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
319 { "srt-port", required_argument, 0, OPTION_SRT_PORT },
321 { "no-srt", no_argument, 0, OPTION_NO_SRT }, // We silently allow this even without HAVE_SRT.
323 { "srt-destination", required_argument, 0, OPTION_SRT_DESTINATION },
324 { "srt-streamid", required_argument, 0, OPTION_SRT_STREAMID },
325 { "srt-passphrase", required_argument, 0, OPTION_SRT_PASSPHRASE },
326 { "srt-youtube-stream-key", required_argument, 0, OPTION_SRT_YOUTUBE_STREAM_KEY },
327 { "srt-latency", required_argument, 0, OPTION_SRT_LATENCY },
329 { "no-transcode-video", no_argument, 0, OPTION_NO_TRANSCODE_VIDEO },
330 { "no-transcode-audio", no_argument, 0, OPTION_NO_TRANSCODE_AUDIO },
331 { "disable-audio", no_argument, 0, OPTION_DISABLE_AUDIO },
332 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
333 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
334 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
335 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
336 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
337 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
338 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
339 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
340 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
341 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
342 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
343 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
344 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
345 { "print-video-latency", no_argument, 0, OPTION_PRINT_VIDEO_LATENCY },
346 { "max-input-queue-frames", required_argument, 0, OPTION_MAX_INPUT_QUEUE_FRAMES },
347 { "audio-queue-length-ms", required_argument, 0, OPTION_AUDIO_QUEUE_LENGTH_MS },
348 { "output-ycbcr-coefficients", required_argument, 0, OPTION_OUTPUT_YCBCR_COEFFICIENTS },
349 { "output-buffer-frames", required_argument, 0, OPTION_OUTPUT_BUFFER_FRAMES },
350 { "output-slop-frames", required_argument, 0, OPTION_OUTPUT_SLOP_FRAMES },
351 { "output-card-unsynchronized", no_argument, 0, OPTION_OUTPUT_CARD_UNSYNCHRONIZED },
352 { "timecode-stream", no_argument, 0, OPTION_TIMECODE_STREAM },
353 { "timecode-stdout", no_argument, 0, OPTION_TIMECODE_STDOUT },
354 { "quick-cut-keys", no_argument, 0, OPTION_QUICK_CUT_KEYS },
355 { "10-bit", no_argument, 0, OPTION_10_BIT },
356 { "input-ycbcr-interpretation", required_argument, 0, OPTION_INPUT_YCBCR_INTERPRETATION },
357 { "mjpeg-export-cards", required_argument, 0, OPTION_MJPEG_EXPORT_CARDS },
359 // Do not explicitly signal texture data uploads (will give
360 // display corruption, but makes it possible to run with
361 // apitrace in real time). For developers only, so undocumented.
362 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
364 // Output speedcontrol debugging statistics. For developers only,
365 // so undocumented (users can get this information from Prometheus
367 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
369 // Deprecated variants.
370 { "10-bit-input", no_argument, 0, OPTION_10_BIT_INPUT },
371 { "10-bit-output", no_argument, 0, OPTION_10_BIT_OUTPUT },
375 vector<string> theme_dirs;
376 string output_ycbcr_coefficients = "auto";
377 bool card_to_mjpeg_stream_export_set = false;
379 int option_index = 0;
380 int c = getopt_long(argc, argv, "c:o:t:I:r:v:m:M:w:h:", long_options, &option_index);
387 global_flags.width = atoi(optarg);
390 global_flags.height = atoi(optarg);
393 global_flags.min_num_cards = atoi(optarg);
395 case OPTION_MAX_NUM_CARDS:
396 global_flags.max_num_cards = atoi(optarg);
399 global_flags.output_card = atoi(optarg);
402 global_flags.theme_filename = optarg;
405 theme_dirs.push_back(optarg);
408 global_flags.recording_dir = optarg;
411 char *ptr = strchr(optarg, ',');
412 if (ptr == nullptr) {
413 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
417 const int signal_num = atoi(optarg);
418 const int card_num = atoi(ptr + 1);
419 if (global_flags.default_stream_mapping.count(signal_num)) {
420 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
421 signal_num, global_flags.default_stream_mapping[signal_num]);
424 global_flags.default_stream_mapping[signal_num] = card_num;
428 global_flags.input_mapping_filename = optarg;
430 case OPTION_MULTICHANNEL:
431 global_flags.multichannel_mapping_mode = true;
434 global_flags.va_display = optarg;
436 case OPTION_MIDI_MAPPING:
437 global_flags.midi_mapping_filename = optarg;
438 global_flags.multichannel_mapping_mode = true;
440 case OPTION_DEFAULT_HDMI_INPUT:
441 global_flags.default_hdmi_input = true;
443 case OPTION_FAKE_CARDS_AUDIO:
444 global_flags.fake_cards_audio = true;
446 case OPTION_V4L_OUTPUT:
447 global_flags.v4l_output_device = optarg;
449 case OPTION_HTTP_MUX:
450 global_flags.stream_mux_name = optarg;
452 case OPTION_HTTP_AUDIO_CODEC:
453 global_flags.stream_audio_codec_name = optarg;
455 case OPTION_HTTP_AUDIO_BITRATE:
456 global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
458 case OPTION_HTTP_PORT:
459 global_flags.http_port = atoi(optarg);
462 case OPTION_SRT_PORT:
463 global_flags.srt_port = atoi(optarg);
467 global_flags.srt_port = -1;
470 case OPTION_SRT_DESTINATION: {
471 const char *ptr = strrchr(optarg, ':');
472 if (ptr == nullptr || strlen(optarg) < 3) {
473 fprintf(stderr, "ERROR: --srt-destination must be of the form host:port\n");
476 global_flags.srt_destination_host = string(optarg, ptr - optarg);
477 if (global_flags.srt_destination_host[0] == '[' &&
478 global_flags.srt_destination_host.back() == ']') {
479 // Support [IPv6]:port, in a sort of hackish way.
480 global_flags.srt_destination_host = global_flags.srt_destination_host.substr(1, global_flags.srt_destination_host.size() - 2);
482 global_flags.srt_destination_port = ptr + 1;
485 case OPTION_SRT_STREAMID:
486 global_flags.srt_streamid = optarg;
488 case OPTION_SRT_PASSPHRASE:
489 global_flags.srt_passphrase = optarg;
491 case OPTION_SRT_YOUTUBE_STREAM_KEY:
492 global_flags.srt_destination_host = "a.srt.youtube.com";
493 global_flags.srt_destination_port = "2010";
494 global_flags.srt_streamid = string("#!::u=") + optarg + ",copy=0,encoder=Nageru " NAGERU_VERSION;
496 case OPTION_SRT_LATENCY:
497 global_flags.srt_output_latency = atoi(optarg);
500 case OPTION_NO_TRANSCODE_VIDEO:
501 global_flags.transcode_video = false;
503 case OPTION_NO_TRANSCODE_AUDIO:
504 global_flags.transcode_audio = false;
506 case OPTION_DISABLE_AUDIO:
507 global_flags.transcode_audio = false;
508 global_flags.enable_audio = false;
510 case OPTION_HTTP_X264_VIDEO:
511 global_flags.x264_video_to_http = true;
513 case OPTION_RECORD_X264_VIDEO:
514 global_flags.x264_video_to_disk = true;
516 case OPTION_SEPARATE_X264_DISK_ENCODE:
517 global_flags.x264_video_to_disk = true;
518 global_flags.x264_video_to_http = true;
519 global_flags.x264_separate_disk_encode = true;
521 case OPTION_HTTP_AV1_VIDEO:
522 global_flags.av1_video_to_http = true;
524 case OPTION_X264_PRESET:
525 global_flags.x264_preset = optarg;
527 case OPTION_X264_TUNE:
528 global_flags.x264_tune = optarg;
530 case OPTION_X264_SPEEDCONTROL:
531 global_flags.x264_speedcontrol = true;
533 case OPTION_X264_SPEEDCONTROL_VERBOSE:
534 global_flags.x264_speedcontrol_verbose = true;
536 case OPTION_X264_BITRATE:
537 global_flags.x264_bitrate = atoi(optarg);
539 case OPTION_X264_CRF:
540 global_flags.x264_crf = atof(optarg);
542 case OPTION_X264_PARAM:
543 global_flags.x264_extra_param.push_back(optarg);
545 case OPTION_X264_SEPARATE_DISK_PRESET:
546 global_flags.x264_separate_disk_preset = optarg;
548 case OPTION_X264_SEPARATE_DISK_TUNE:
549 global_flags.x264_separate_disk_tune = optarg;
551 case OPTION_X264_SEPARATE_DISK_BITRATE:
552 global_flags.x264_separate_disk_bitrate = atoi(optarg);
554 case OPTION_X264_SEPARATE_DISK_CRF:
555 global_flags.x264_separate_disk_crf = atof(optarg);
557 case OPTION_X264_SEPARATE_DISK_PARAM:
558 global_flags.x264_separate_disk_extra_param.push_back(optarg);
560 case OPTION_AV1_PRESET:
561 global_flags.av1_preset = atoi(optarg);
563 case OPTION_AV1_BITRATE:
564 global_flags.av1_bitrate = atoi(optarg);
566 case OPTION_AV1_FPS: {
568 const size_t pos = str.find('/');
569 if (pos == string::npos) {
570 global_flags.av1_fps_num = stoi(str);
571 global_flags.av1_fps_den = 1;
573 const string num = str.substr(0, pos);
574 const string den = str.substr(pos + 1);
575 global_flags.av1_fps_num = stoi(num);
576 global_flags.av1_fps_den = stoi(den);
580 case OPTION_AV1_PARAM:
581 global_flags.av1_extra_param.push_back(optarg);
583 case OPTION_FLAT_AUDIO:
584 // If --flat-audio is given, turn off everything that messes with the sound,
585 // except the final makeup gain.
586 global_flags.locut_enabled = false;
587 global_flags.gain_staging_auto = false;
588 global_flags.compressor_enabled = false;
589 global_flags.limiter_enabled = false;
591 case OPTION_GAIN_STAGING:
592 global_flags.initial_gain_staging_db = atof(optarg);
593 global_flags.gain_staging_auto = false;
595 case OPTION_DISABLE_LOCUT:
596 global_flags.locut_enabled = false;
598 case OPTION_ENABLE_LOCUT:
599 global_flags.locut_enabled = true;
601 case OPTION_DISABLE_GAIN_STAGING_AUTO:
602 global_flags.gain_staging_auto = false;
604 case OPTION_ENABLE_GAIN_STAGING_AUTO:
605 global_flags.gain_staging_auto = true;
607 case OPTION_DISABLE_COMPRESSOR:
608 global_flags.compressor_enabled = false;
610 case OPTION_ENABLE_COMPRESSOR:
611 global_flags.compressor_enabled = true;
613 case OPTION_DISABLE_LIMITER:
614 global_flags.limiter_enabled = false;
616 case OPTION_ENABLE_LIMITER:
617 global_flags.limiter_enabled = true;
619 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
620 global_flags.final_makeup_gain_auto = false;
622 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
623 global_flags.final_makeup_gain_auto = true;
625 case OPTION_DISABLE_ALSA_OUTPUT:
626 global_flags.enable_alsa_output = false;
628 case OPTION_NO_FLUSH_PBOS:
629 global_flags.flush_pbos = false;
631 case OPTION_PRINT_VIDEO_LATENCY:
632 global_flags.print_video_latency = true;
634 case OPTION_MAX_INPUT_QUEUE_FRAMES:
635 global_flags.max_input_queue_frames = atoi(optarg);
637 case OPTION_AUDIO_QUEUE_LENGTH_MS:
638 global_flags.audio_queue_length_ms = atof(optarg);
640 case OPTION_OUTPUT_YCBCR_COEFFICIENTS:
641 output_ycbcr_coefficients = optarg;
643 case OPTION_OUTPUT_BUFFER_FRAMES:
644 global_flags.output_buffer_frames = atof(optarg);
646 case OPTION_OUTPUT_SLOP_FRAMES:
647 global_flags.output_slop_frames = atof(optarg);
649 case OPTION_OUTPUT_CARD_UNSYNCHRONIZED:
650 global_flags.output_card_is_master = false;
652 case OPTION_TIMECODE_STREAM:
653 global_flags.display_timecode_in_stream = true;
655 case OPTION_TIMECODE_STDOUT:
656 global_flags.display_timecode_on_stdout = true;
658 case OPTION_QUICK_CUT_KEYS:
659 global_flags.enable_quick_cut_keys = true;
661 case OPTION_10_BIT_INPUT:
662 fprintf(stderr, "NOTE: --10-bit-input is a deprecated alias for --10-bit.\n");
663 global_flags.bit_depth = 10;
665 case OPTION_10_BIT_OUTPUT:
666 fprintf(stderr, "NOTE: --10-bit-output is a deprecated alias for --10-bit.\n");
667 global_flags.bit_depth = 10;
670 global_flags.bit_depth = 10;
672 case OPTION_INPUT_YCBCR_INTERPRETATION: {
673 char *ptr = strchr(optarg, ',');
674 if (ptr == nullptr) {
675 fprintf(stderr, "ERROR: Invalid argument '%s' to --input-ycbcr-interpretation (needs a card and an interpretation, separated by comma)\n", optarg);
679 const int card_num = atoi(optarg);
680 if (card_num < 0 || card_num >= MAX_VIDEO_CARDS) {
681 fprintf(stderr, "ERROR: Invalid card number %d\n", card_num);
685 YCbCrInterpretation interpretation;
686 char *interpretation_str = ptr + 1;
687 ptr = strchr(interpretation_str, ',');
688 if (ptr != nullptr) {
690 const char *range = ptr + 1;
691 if (strcmp(range, "full") == 0) {
692 interpretation.full_range = true;
693 } else if (strcmp(range, "limited") == 0) {
694 interpretation.full_range = false;
696 fprintf(stderr, "ERROR: Invalid Y'CbCr range '%s' (must be “full” or “limited”)\n", range);
701 if (strcmp(interpretation_str, "rec601") == 0) {
702 interpretation.ycbcr_coefficients_auto = false;
703 interpretation.ycbcr_coefficients = movit::YCBCR_REC_601;
704 } else if (strcmp(interpretation_str, "rec709") == 0) {
705 interpretation.ycbcr_coefficients_auto = false;
706 interpretation.ycbcr_coefficients = movit::YCBCR_REC_709;
707 } else if (strcmp(interpretation_str, "auto") == 0) {
708 interpretation.ycbcr_coefficients_auto = true;
709 if (interpretation.full_range) {
710 fprintf(stderr, "ERROR: Cannot use “auto” Y'CbCr coefficients with full range\n");
714 fprintf(stderr, "ERROR: Invalid Y'CbCr coefficients '%s' (must be “rec601”, “rec709” or “auto”)\n", interpretation_str);
717 global_flags.ycbcr_interpretation[card_num] = interpretation;
720 case OPTION_FULLSCREEN:
721 global_flags.fullscreen = true;
723 case OPTION_MJPEG_EXPORT_CARDS: {
724 if (card_to_mjpeg_stream_export_set) {
725 fprintf(stderr, "ERROR: --mjpeg-export-cards given twice\n");
728 global_flags.card_to_mjpeg_stream_export = parse_mjpeg_export_cards(optarg);
729 card_to_mjpeg_stream_export_set = true;
736 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
737 fprintf(stderr, "\n");
743 if (global_flags.x264_video_to_http +
744 global_flags.av1_video_to_http > 1) {
745 fprintf(stderr, "ERROR: --http-{x264,av1}-video are mutually incompatible\n");
748 if (global_flags.bit_depth == 10) {
749 global_flags.x264_video_to_disk = true; // No 10-bit Quick Sync support.
750 if (!global_flags.av1_video_to_http) {
751 global_flags.x264_video_to_http = true;
754 if (global_flags.x264_video_to_disk && !global_flags.av1_video_to_http) {
755 global_flags.x264_video_to_http = true; // Quick Sync to HTTP but x264 to disk doesn't make sense.
757 if (global_flags.min_num_cards <= 0) {
758 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
761 if (global_flags.max_num_cards <= 0) {
762 fprintf(stderr, "ERROR: --max-num-cards must be at least 1\n");
765 if (global_flags.max_num_cards < global_flags.min_num_cards) {
766 fprintf(stderr, "ERROR: --max-num-cards can not be lower than --num-cards\n");
769 if (global_flags.output_card < -1 ||
770 global_flags.output_card >= global_flags.max_num_cards) {
771 fprintf(stderr, "ERROR: --output-card points to a nonexistant card\n");
774 if (global_flags.enable_audio && !global_flags.transcode_audio && global_flags.stream_audio_codec_name.empty()) {
775 fprintf(stderr, "ERROR: If not transcoding audio, you must specify ahead-of-time what audio codec is in use\n");
776 fprintf(stderr, " (using --http-audio-codec).\n");
779 if (global_flags.x264_speedcontrol) {
780 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
781 fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
783 global_flags.x264_preset = "faster";
784 } else if (global_flags.x264_preset.empty()) {
785 global_flags.x264_preset = X264_DEFAULT_PRESET;
787 if (global_flags.x264_separate_disk_preset.empty()) {
788 global_flags.x264_separate_disk_preset = X264_DEFAULT_PRESET;
790 if (!theme_dirs.empty()) {
791 global_flags.theme_dirs = theme_dirs;
794 // In reality, we could probably do with any even value (we subsample
795 // by two in some places), but it's better to be on the safe side
796 // wrt. video codecs and such. (I'd set 16 if I could, but 1080 isn't
798 if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
799 global_flags.height <= 0 || (global_flags.height % 8) != 0) {
800 fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
804 for (pair<int, int> mapping : global_flags.default_stream_mapping) {
805 if (mapping.second >= global_flags.max_num_cards) {
806 fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --max-num-cards)\n",
807 mapping.first, mapping.second);
812 // Rec. 709 would be the sane thing to do, but it seems many players
813 // just default to BT.601 coefficients no matter what. We _do_ set
814 // the right flags, so that a player that works properly doesn't have
815 // to guess, but it's frequently ignored. See discussions
816 // in e.g. https://trac.ffmpeg.org/ticket/4978; the situation with
817 // browsers is complicated and depends on things like hardware acceleration
818 // (https://bugs.chromium.org/p/chromium/issues/detail?id=333619 for
819 // extensive discussion). VLC generally fixed this as part of 3.0.0
820 // (see e.g. https://github.com/videolan/vlc/commit/bc71288b2e38c07d6921472824b92eef1aa85f7e
821 // and https://github.com/videolan/vlc/commit/c3fc2683a9cde1d42674ebf9935dced05733a215),
822 // but earlier versions were pretty random.
824 // On the other hand, HDMI/SDI output typically requires Rec. 709 for
825 // HD resolutions (with no way of signaling anything else), which is
826 // a conflicting demand. In this case, we typically let the HDMI/SDI
827 // output win if it is active, but the user can override this.
828 if (output_ycbcr_coefficients == "auto") {
829 // Essentially: BT.709 if HDMI/SDI output is on, otherwise BT.601.
830 global_flags.ycbcr_rec709_coefficients = false;
831 global_flags.ycbcr_auto_coefficients = true;
832 } else if (output_ycbcr_coefficients == "rec709") {
833 global_flags.ycbcr_rec709_coefficients = true;
834 global_flags.ycbcr_auto_coefficients = false;
835 } else if (output_ycbcr_coefficients == "rec601") {
836 global_flags.ycbcr_rec709_coefficients = false;
837 global_flags.ycbcr_auto_coefficients = false;
839 fprintf(stderr, "ERROR: --output-ycbcr-coefficients must be “rec601”, “rec709” or “auto”\n");
843 if (global_flags.output_buffer_frames < 0.0f) {
844 // Actually, even zero probably won't make sense; there is some internal
845 // delay to the card.
846 fprintf(stderr, "ERROR: --output-buffer-frames can't be negative.\n");
849 if (global_flags.output_slop_frames < 0.0f) {
850 fprintf(stderr, "ERROR: --output-slop-frames can't be negative.\n");
853 if (global_flags.max_input_queue_frames < 1) {
854 fprintf(stderr, "ERROR: --max-input-queue-frames must be at least 1.\n");
857 if (global_flags.max_input_queue_frames > 10) {
858 fprintf(stderr, "WARNING: --max-input-queue-frames has little effect over 10.\n");
861 if (!isinf(global_flags.x264_crf)) { // CRF mode is selected.
862 if (global_flags.x264_bitrate != -1) {
863 fprintf(stderr, "ERROR: --x264-bitrate and --x264-crf are mutually incompatible.\n");
866 } else if (global_flags.x264_bitrate == -1) {
867 global_flags.x264_bitrate = DEFAULT_X264_OUTPUT_BIT_RATE;
870 if (!isinf(global_flags.x264_separate_disk_crf)) { // CRF mode is selected.
871 if (global_flags.x264_separate_disk_bitrate != -1) {
872 fprintf(stderr, "ERROR: --x264-separate-disk-bitrate and --x264-separate-disk-crf are mutually incompatible.\n");
875 } else if (global_flags.x264_separate_disk_bitrate == -1) {
876 global_flags.x264_separate_disk_bitrate = DEFAULT_X264_OUTPUT_BIT_RATE;
879 if (!card_to_mjpeg_stream_export_set) {
880 // Fill in the default mapping (export all cards, in order).
881 for (unsigned card_idx = 0; card_idx < unsigned(global_flags.max_num_cards); ++card_idx) {
882 global_flags.card_to_mjpeg_stream_export[card_idx] = card_idx;
886 if (global_flags.srt_destination_host == "a.srt.youtube.com" && global_flags.srt_passphrase.empty()) {
887 fprintf(stderr, "ERROR: Cannot stream to YouTube without --srt-passphrase (get it from the YouTube streaming console)\n");