]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
Persist quality settings in the database.
[nageru] / futatabi / 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 int flow_initialized_interpolation_quality;
14
15 // Long options that have no corresponding short option.
16 enum LongOption {
17         OPTION_HELP = 1000,
18         OPTION_SLOW_DOWN_INPUT = 1001,
19         OPTION_HTTP_PORT = 1002
20 };
21
22 void usage()
23 {
24         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
25         fprintf(stderr, "\n");
26         fprintf(stderr, "      --help                      print usage information\n");
27         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
28         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
29         fprintf(stderr, "  -r, --frame-rate NUM[/NUM]      output frame rate, as a float or fraction\n");
30         fprintf(stderr, "                                    (default 60000/1001 ~= 59.94)\n");
31         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
32         fprintf(stderr, "                                    source URL given)\n");
33         fprintf(stderr, "  -q, --interpolation-quality N   0 = off\n");
34         fprintf(stderr, "                                  1 = fastest\n");
35         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
36         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
37         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
38         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
39         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
40 }
41
42 void parse_flags(int argc, char * const argv[])
43 {
44         static const option long_options[] = {
45                 { "help", no_argument, 0, OPTION_HELP },
46                 { "width", required_argument, 0, 'w' },
47                 { "height", required_argument, 0, 'h' },
48                 { "frame-rate", required_argument, 0, 'r' },
49                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
50                 { "interpolation-quality", required_argument, 0, 'q' },
51                 { "working-directory", required_argument, 0, 'd' },
52                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
53                 { 0, 0, 0, 0 }
54         };
55         for ( ;; ) {
56                 int option_index = 0;
57                 int c = getopt_long(argc, argv, "w:h:r:q:d:", long_options, &option_index);
58
59                 if (c == -1) {
60                         break;
61                 }
62                 switch (c) {
63                 case 'w':
64                         global_flags.width = atoi(optarg);
65                         break;
66                 case 'h':
67                         global_flags.height = atoi(optarg);
68                         break;
69                 case 'r': {
70                         double num, den;
71                         if (sscanf(optarg, "%lf/%lf", &num, &den) == 2) {
72                                 global_flags.output_framerate = num / den;
73                         } else if (sscanf(optarg, "%lf", &num) == 1) {
74                                 global_flags.output_framerate = num;
75                         } else {
76                                 fprintf(stderr, "Invalid frame rate given (must be on the form N or N/M)\n");
77                                 exit(1);
78                         }
79                         break;
80                 }
81                 case OPTION_SLOW_DOWN_INPUT:
82                         global_flags.slow_down_input = true;
83                         break;
84                 case 'q':
85                         global_flags.interpolation_quality = atoi(optarg);
86                         global_flags.interpolation_quality_set = true;
87                         break;
88                 case 'd':
89                         global_flags.working_directory = optarg;
90                         break;
91                 case OPTION_HTTP_PORT:
92                         global_flags.http_port = atoi(optarg);
93                         break;
94                 case OPTION_HELP:
95                         usage();
96                         exit(0);
97                 default:
98                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
99                         fprintf(stderr, "\n");
100                         usage();
101                         exit(1);
102                 }
103         }
104
105         if (global_flags.interpolation_quality < 0 || global_flags.interpolation_quality > 4) {
106                 fprintf(stderr, "Interpolation quality must be 0, 1, 2, 3 or 4.\n");
107                 usage();
108                 exit(1);
109         }
110 }