]> git.sesse.net Git - nageru/blob - flags.cpp
Add a flag to output Y'CbCr using Rec. 709 coefficients.
[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_X264_PRESET,
23         OPTION_X264_TUNE,
24         OPTION_X264_SPEEDCONTROL,
25         OPTION_X264_SPEEDCONTROL_VERBOSE,
26         OPTION_X264_BITRATE,
27         OPTION_X264_VBV_BUFSIZE,
28         OPTION_X264_VBV_MAX_BITRATE,
29         OPTION_X264_PARAM,
30         OPTION_HTTP_MUX,
31         OPTION_HTTP_COARSE_TIMEBASE,
32         OPTION_HTTP_AUDIO_CODEC,
33         OPTION_HTTP_AUDIO_BITRATE,
34         OPTION_FLAT_AUDIO,
35         OPTION_GAIN_STAGING,
36         OPTION_DISABLE_LOCUT,
37         OPTION_ENABLE_LOCUT,
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,
47         OPTION_NO_FLUSH_PBOS,
48         OPTION_PRINT_VIDEO_LATENCY,
49         OPTION_AUDIO_QUEUE_LENGTH_MS,
50         OPTION_OUTPUT_YCBCR_COEFFICIENTS
51 };
52
53 void usage()
54 {
55         fprintf(stderr, "Usage: nageru [OPTION]...\n");
56         fprintf(stderr, "\n");
57         fprintf(stderr, "      --help                      print usage information\n");
58         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
59         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
60         fprintf(stderr, "  -c, --num-cards                 set number of input cards (default 2)\n");
61         fprintf(stderr, "  -t, --theme=FILE                choose theme (default theme.lua)\n");
62         fprintf(stderr, "  -I, --theme-dir=DIR             search for theme in this directory (can be given multiple times)\n");
63         fprintf(stderr, "  -v, --va-display=SPEC           VA-API device for H.264 encoding\n");
64         fprintf(stderr, "                                    ($DISPLAY spec or /dev/dri/render* path)\n");
65         fprintf(stderr, "  -m, --map-signal=SIGNAL,CARD    set a default card mapping (can be given multiple times)\n");
66         fprintf(stderr, "  -M, --input-mapping=FILE        start with the given audio input mapping (implies --multichannel)\n");
67         fprintf(stderr, "      --multichannel              start in multichannel audio mapping mode\n");
68         fprintf(stderr, "      --midi-mapping=FILE         start with the given MIDI controller mapping (implies --multichannel)\n");
69         fprintf(stderr, "      --fake-cards-audio          make fake (disconnected) cards output a simple tone\n");
70         fprintf(stderr, "      --http-uncompressed-video   send uncompressed NV12 video to HTTP clients\n");
71         fprintf(stderr, "      --http-x264-video           send x264-compressed video to HTTP clients\n");
72         fprintf(stderr, "      --x264-preset               x264 quality preset (default " X264_DEFAULT_PRESET ")\n");
73         fprintf(stderr, "      --x264-tune                 x264 tuning (default " X264_DEFAULT_TUNE ", can be blank)\n");
74         fprintf(stderr, "      --x264-speedcontrol         try to match x264 preset to available CPU speed\n");
75         fprintf(stderr, "      --x264-speedcontrol-verbose  output speedcontrol debugging statistics\n");
76         fprintf(stderr, "      --x264-bitrate              x264 bitrate (in kilobit/sec, default %d)\n",
77                 DEFAULT_X264_OUTPUT_BIT_RATE);
78         fprintf(stderr, "      --x264-vbv-bufsize          x264 VBV size (in kilobits, 0 = one-frame VBV,\n");
79         fprintf(stderr, "                                  default: same as --x264-bitrate, that is, one-second VBV)\n");
80         fprintf(stderr, "      --x264-vbv-max-bitrate      x264 local max bitrate (in kilobit/sec per --vbv-bufsize,\n");
81         fprintf(stderr, "                                  0 = no limit, default: same as --x264-bitrate, i.e., CBR)\n");
82         fprintf(stderr, "      --x264-param=NAME[,VALUE]   set any x264 parameter, for fine tuning\n");
83         fprintf(stderr, "      --http-mux=NAME             mux to use for HTTP streams (default " DEFAULT_STREAM_MUX_NAME ")\n");
84         fprintf(stderr, "      --http-audio-codec=NAME     audio codec to use for HTTP streams\n");
85         fprintf(stderr, "                                  (default is to use the same as for the recording)\n");
86         fprintf(stderr, "      --http-audio-bitrate=KBITS  audio codec bit rate to use for HTTP streams\n");
87         fprintf(stderr, "                                  (default is %d, ignored unless --http-audio-codec is set)\n",
88                 DEFAULT_AUDIO_OUTPUT_BIT_RATE / 1000);
89         fprintf(stderr, "      --http-coarse-timebase      use less timebase for HTTP (recommended for muxers\n");
90         fprintf(stderr, "                                  that handle large pts poorly, like e.g. MP4)\n");
91         fprintf(stderr, "      --flat-audio                start with most audio processing turned off\n");
92         fprintf(stderr, "                                    (can be overridden by e.g. --enable-limiter)\n");
93         fprintf(stderr, "      --gain-staging=DB           set initial gain staging to the given value\n");
94         fprintf(stderr, "                                    (--disable-gain-staging-auto)\n");
95         fprintf(stderr, "      --disable-locut             turn off locut filter (also --enable)\n");
96         fprintf(stderr, "      --disable-gain-staging-auto  turn off automatic gain staging (also --enable)\n");
97         fprintf(stderr, "      --disable-compressor        turn off regular compressor (also --enable)\n");
98         fprintf(stderr, "      --disable-limiter           turn off limiter (also --enable)\n");
99         fprintf(stderr, "      --disable-makeup-gain-auto  turn off auto-adjustment of final makeup gain (also --enable)\n");
100         fprintf(stderr, "      --disable-alsa-output       disable audio monitoring via ALSA\n");
101         fprintf(stderr, "      --no-flush-pbos             do not explicitly signal texture data uploads\n");
102         fprintf(stderr, "                                    (will give display corruption, but makes it\n");
103         fprintf(stderr, "                                    possible to run with apitrace in real time)\n");
104         fprintf(stderr, "      --print-video-latency       print out measurements of video latency on stdout\n");
105         fprintf(stderr, "      --audio-queue-length-ms     length of audio resampling queue (default 100.0)\n");
106         fprintf(stderr, "      --output-ycbcr-coefficients={rec601,rec709}\n");
107         fprintf(stderr, "                                  Y'CbCr coefficient standard of output (default rec601)\n");
108 }
109
110 void parse_flags(int argc, char * const argv[])
111 {
112         static const option long_options[] = {
113                 { "help", no_argument, 0, OPTION_HELP },
114                 { "width", required_argument, 0, 'w' },
115                 { "height", required_argument, 0, 'h' },
116                 { "num-cards", required_argument, 0, 'c' },
117                 { "theme", required_argument, 0, 't' },
118                 { "theme-dir", required_argument, 0, 'I' },
119                 { "map-signal", required_argument, 0, 'm' },
120                 { "input-mapping", required_argument, 0, 'M' },
121                 { "va-display", required_argument, 0, 'v' },
122                 { "multichannel", no_argument, 0, OPTION_MULTICHANNEL },
123                 { "midi-mapping", required_argument, 0, OPTION_MIDI_MAPPING },
124                 { "fake-cards-audio", no_argument, 0, OPTION_FAKE_CARDS_AUDIO },
125                 { "http-uncompressed-video", no_argument, 0, OPTION_HTTP_UNCOMPRESSED_VIDEO },
126                 { "http-x264-video", no_argument, 0, OPTION_HTTP_X264_VIDEO },
127                 { "x264-preset", required_argument, 0, OPTION_X264_PRESET },
128                 { "x264-tune", required_argument, 0, OPTION_X264_TUNE },
129                 { "x264-speedcontrol", no_argument, 0, OPTION_X264_SPEEDCONTROL },
130                 { "x264-speedcontrol-verbose", no_argument, 0, OPTION_X264_SPEEDCONTROL_VERBOSE },
131                 { "x264-bitrate", required_argument, 0, OPTION_X264_BITRATE },
132                 { "x264-vbv-bufsize", required_argument, 0, OPTION_X264_VBV_BUFSIZE },
133                 { "x264-vbv-max-bitrate", required_argument, 0, OPTION_X264_VBV_MAX_BITRATE },
134                 { "x264-param", required_argument, 0, OPTION_X264_PARAM },
135                 { "http-mux", required_argument, 0, OPTION_HTTP_MUX },
136                 { "http-coarse-timebase", no_argument, 0, OPTION_HTTP_COARSE_TIMEBASE },
137                 { "http-audio-codec", required_argument, 0, OPTION_HTTP_AUDIO_CODEC },
138                 { "http-audio-bitrate", required_argument, 0, OPTION_HTTP_AUDIO_BITRATE },
139                 { "flat-audio", no_argument, 0, OPTION_FLAT_AUDIO },
140                 { "gain-staging", required_argument, 0, OPTION_GAIN_STAGING },
141                 { "disable-locut", no_argument, 0, OPTION_DISABLE_LOCUT },
142                 { "enable-locut", no_argument, 0, OPTION_ENABLE_LOCUT },
143                 { "disable-gain-staging-auto", no_argument, 0, OPTION_DISABLE_GAIN_STAGING_AUTO },
144                 { "enable-gain-staging-auto", no_argument, 0, OPTION_ENABLE_GAIN_STAGING_AUTO },
145                 { "disable-compressor", no_argument, 0, OPTION_DISABLE_COMPRESSOR },
146                 { "enable-compressor", no_argument, 0, OPTION_ENABLE_COMPRESSOR },
147                 { "disable-limiter", no_argument, 0, OPTION_DISABLE_LIMITER },
148                 { "enable-limiter", no_argument, 0, OPTION_ENABLE_LIMITER },
149                 { "disable-makeup-gain-auto", no_argument, 0, OPTION_DISABLE_MAKEUP_GAIN_AUTO },
150                 { "enable-makeup-gain-auto", no_argument, 0, OPTION_ENABLE_MAKEUP_GAIN_AUTO },
151                 { "disable-alsa-output", no_argument, 0, OPTION_DISABLE_ALSA_OUTPUT },
152                 { "no-flush-pbos", no_argument, 0, OPTION_NO_FLUSH_PBOS },
153                 { "print-video-latency", no_argument, 0, OPTION_PRINT_VIDEO_LATENCY },
154                 { "audio-queue-length-ms", required_argument, 0, OPTION_AUDIO_QUEUE_LENGTH_MS },
155                 { "output-ycbcr-coefficients", required_argument, 0, OPTION_OUTPUT_YCBCR_COEFFICIENTS },
156                 { 0, 0, 0, 0 }
157         };
158         vector<string> theme_dirs;
159         string output_ycbcr_coefficients = "rec601";
160         for ( ;; ) {
161                 int option_index = 0;
162                 int c = getopt_long(argc, argv, "c:t:I:v:m:M:w:h:", long_options, &option_index);
163
164                 if (c == -1) {
165                         break;
166                 }
167                 switch (c) {
168                 case 'w':
169                         global_flags.width = atoi(optarg);
170                         break;
171                 case 'h':
172                         global_flags.height = atoi(optarg);
173                         break;
174                 case 'c':
175                         global_flags.num_cards = atoi(optarg);
176                         break;
177                 case 't':
178                         global_flags.theme_filename = optarg;
179                         break;
180                 case 'I':
181                         theme_dirs.push_back(optarg);
182                         break;
183                 case 'm': {
184                         char *ptr = strchr(optarg, ',');
185                         if (ptr == nullptr) {
186                                 fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
187                                 exit(1);
188                         }
189                         *ptr = '\0';
190                         const int signal_num = atoi(optarg);
191                         const int card_num = atoi(ptr + 1);
192                         if (global_flags.default_stream_mapping.count(signal_num)) {
193                                 fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
194                                         signal_num, global_flags.default_stream_mapping[signal_num]);
195                                 exit(1);
196                         }
197                         global_flags.default_stream_mapping[signal_num] = card_num;
198                         break;
199                 }
200                 case 'M':
201                         global_flags.input_mapping_filename = optarg;
202                         break;
203                 case OPTION_MULTICHANNEL:
204                         global_flags.multichannel_mapping_mode = true;
205                         break;
206                 case 'v':
207                         global_flags.va_display = optarg;
208                         break;
209                 case OPTION_MIDI_MAPPING:
210                         global_flags.midi_mapping_filename = optarg;
211                         global_flags.multichannel_mapping_mode = true;
212                         break;
213                 case OPTION_FAKE_CARDS_AUDIO:
214                         global_flags.fake_cards_audio = true;
215                         break;
216                 case OPTION_HTTP_UNCOMPRESSED_VIDEO:
217                         global_flags.uncompressed_video_to_http = true;
218                         break;
219                 case OPTION_HTTP_MUX:
220                         global_flags.stream_mux_name = optarg;
221                         break;
222                 case OPTION_HTTP_COARSE_TIMEBASE:
223                         global_flags.stream_coarse_timebase = true;
224                         break;
225                 case OPTION_HTTP_AUDIO_CODEC:
226                         global_flags.stream_audio_codec_name = optarg;
227                         break;
228                 case OPTION_HTTP_AUDIO_BITRATE:
229                         global_flags.stream_audio_codec_bitrate = atoi(optarg) * 1000;
230                         break;
231                 case OPTION_HTTP_X264_VIDEO:
232                         global_flags.x264_video_to_http = true;
233                         break;
234                 case OPTION_X264_PRESET:
235                         global_flags.x264_preset = optarg;
236                         break;
237                 case OPTION_X264_TUNE:
238                         global_flags.x264_tune = optarg;
239                         break;
240                 case OPTION_X264_SPEEDCONTROL:
241                         global_flags.x264_speedcontrol = true;
242                         break;
243                 case OPTION_X264_SPEEDCONTROL_VERBOSE:
244                         global_flags.x264_speedcontrol_verbose = true;
245                         break;
246                 case OPTION_X264_BITRATE:
247                         global_flags.x264_bitrate = atoi(optarg);
248                         break;
249                 case OPTION_X264_VBV_BUFSIZE:
250                         global_flags.x264_vbv_buffer_size = atoi(optarg);
251                         break;
252                 case OPTION_X264_VBV_MAX_BITRATE:
253                         global_flags.x264_vbv_max_bitrate = atoi(optarg);
254                         break;
255                 case OPTION_X264_PARAM:
256                         global_flags.x264_extra_param.push_back(optarg);
257                         break;
258                 case OPTION_FLAT_AUDIO:
259                         // If --flat-audio is given, turn off everything that messes with the sound,
260                         // except the final makeup gain.
261                         global_flags.locut_enabled = false;
262                         global_flags.gain_staging_auto = false;
263                         global_flags.compressor_enabled = false;
264                         global_flags.limiter_enabled = false;
265                         break;
266                 case OPTION_GAIN_STAGING:
267                         global_flags.initial_gain_staging_db = atof(optarg);
268                         global_flags.gain_staging_auto = false;
269                         break;
270                 case OPTION_DISABLE_LOCUT:
271                         global_flags.locut_enabled = false;
272                         break;
273                 case OPTION_ENABLE_LOCUT:
274                         global_flags.locut_enabled = true;
275                         break;
276                 case OPTION_DISABLE_GAIN_STAGING_AUTO:
277                         global_flags.gain_staging_auto = false;
278                         break;
279                 case OPTION_ENABLE_GAIN_STAGING_AUTO:
280                         global_flags.gain_staging_auto = true;
281                         break;
282                 case OPTION_DISABLE_COMPRESSOR:
283                         global_flags.compressor_enabled = false;
284                         break;
285                 case OPTION_ENABLE_COMPRESSOR:
286                         global_flags.compressor_enabled = true;
287                         break;
288                 case OPTION_DISABLE_LIMITER:
289                         global_flags.limiter_enabled = false;
290                         break;
291                 case OPTION_ENABLE_LIMITER:
292                         global_flags.limiter_enabled = true;
293                         break;
294                 case OPTION_DISABLE_MAKEUP_GAIN_AUTO:
295                         global_flags.final_makeup_gain_auto = false;
296                         break;
297                 case OPTION_ENABLE_MAKEUP_GAIN_AUTO:
298                         global_flags.final_makeup_gain_auto = true;
299                         break;
300                 case OPTION_DISABLE_ALSA_OUTPUT:
301                         global_flags.enable_alsa_output = false;
302                         break;
303                 case OPTION_NO_FLUSH_PBOS:
304                         global_flags.flush_pbos = false;
305                         break;
306                 case OPTION_PRINT_VIDEO_LATENCY:
307                         global_flags.print_video_latency = true;
308                         break;
309                 case OPTION_AUDIO_QUEUE_LENGTH_MS:
310                         global_flags.audio_queue_length_ms = atof(optarg);
311                         break;
312                 case OPTION_OUTPUT_YCBCR_COEFFICIENTS:
313                         output_ycbcr_coefficients = optarg;
314                         break;
315                 case OPTION_HELP:
316                         usage();
317                         exit(0);
318                 default:
319                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
320                         fprintf(stderr, "\n");
321                         usage();
322                         exit(1);
323                 }
324         }
325
326         if (global_flags.uncompressed_video_to_http &&
327             global_flags.x264_video_to_http) {
328                 fprintf(stderr, "ERROR: --http-uncompressed-video and --http-x264-video are mutually incompatible\n");
329                 exit(1);
330         }
331         if (global_flags.num_cards <= 0) {
332                 fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
333                 exit(1);
334         }
335         if (global_flags.x264_speedcontrol) {
336                 if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
337                         fprintf(stderr, "WARNING: --x264-preset is overridden by --x264-speedcontrol (implicitly uses \"faster\" as base preset)\n");
338                 }
339                 global_flags.x264_preset = "faster";
340         } else if (global_flags.x264_preset.empty()) {
341                 global_flags.x264_preset = X264_DEFAULT_PRESET;
342         }
343         if (!theme_dirs.empty()) {
344                 global_flags.theme_dirs = theme_dirs;
345         }
346
347         // In reality, we could probably do with any even value (we subsample
348         // by two in some places), but it's better to be on the safe side
349         // wrt. video codecs and such. (I'd set 16 if I could, but 1080 isn't
350         // divisible by 16.)
351         if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
352             global_flags.height <= 0 || (global_flags.height % 8) != 0) {
353                 fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
354                 exit(1);
355         }
356
357         for (pair<int, int> mapping : global_flags.default_stream_mapping) {
358                 if (mapping.second >= global_flags.num_cards) {
359                         fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --num-cards)\n",
360                                 mapping.first, mapping.second);
361                         exit(1);
362                 }
363         }
364
365         if (output_ycbcr_coefficients == "rec709") {
366                 global_flags.ycbcr_rec709_coefficients = true;
367         } else if (output_ycbcr_coefficients == "rec601") {
368                 global_flags.ycbcr_rec709_coefficients = false;
369         } else {
370                 fprintf(stderr, "ERROR: --output-ycbcr-coefficients must be “rec601” or “rec709”\n");
371                 exit(1);
372         }
373 }