]> git.sesse.net Git - nageru/blob - futatabi/flags.cpp
Add cue point padding, where a clip is elongated (in both directions) by some given...
[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         OPTION_TALLY_URL = 1003,
21         OPTION_CUE_POINT_PADDING = 1004
22 };
23
24 void usage()
25 {
26         fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n");
27         fprintf(stderr, "\n");
28         fprintf(stderr, "      --help                      print usage information\n");
29         fprintf(stderr, "  -w, --width                     output width in pixels (default 1280)\n");
30         fprintf(stderr, "  -h, --height                    output height in pixels (default 720)\n");
31         fprintf(stderr, "  -r, --frame-rate NUM[/NUM]      output frame rate, as a float or fraction\n");
32         fprintf(stderr, "                                    (default 60000/1001 ~= 59.94)\n");
33         fprintf(stderr, "      --slow-down-input           slow down input to realtime (default on if no\n");
34         fprintf(stderr, "                                    source URL given)\n");
35         fprintf(stderr, "  -q, --interpolation-quality N   0 = off\n");
36         fprintf(stderr, "                                  1 = fastest\n");
37         fprintf(stderr, "                                  2 = default (realtime 720p on fast embedded GPUs)\n");
38         fprintf(stderr, "                                  3 = good (realtime 720p on GTX 970 or so)\n");
39         fprintf(stderr, "                                  4 = best (not realtime on any current GPU)\n");
40         fprintf(stderr, "      --cue-point-padding SECS    move cue-in/cue-out N seconds earlier/later on set\n");
41         fprintf(stderr, "  -d, --working-directory DIR     where to store frames and database\n");
42         fprintf(stderr, "      --http-port PORT            which port to listen on for output\n");
43         fprintf(stderr, "      --tally-url URL             URL to get tally color from (polled every 100 ms)\n");
44 }
45
46 void parse_flags(int argc, char * const argv[])
47 {
48         static const option long_options[] = {
49                 { "help", no_argument, 0, OPTION_HELP },
50                 { "width", required_argument, 0, 'w' },
51                 { "height", required_argument, 0, 'h' },
52                 { "frame-rate", required_argument, 0, 'r' },
53                 { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT },
54                 { "interpolation-quality", required_argument, 0, 'q' },
55                 { "working-directory", required_argument, 0, 'd' },
56                 { "http-port", required_argument, 0, OPTION_HTTP_PORT },
57                 { "tally-url", required_argument, 0, OPTION_TALLY_URL },
58                 { "cue-point-padding", required_argument, 0, OPTION_CUE_POINT_PADDING },
59                 { 0, 0, 0, 0 }
60         };
61         for ( ;; ) {
62                 int option_index = 0;
63                 int c = getopt_long(argc, argv, "w:h:r:q:d:", long_options, &option_index);
64
65                 if (c == -1) {
66                         break;
67                 }
68                 switch (c) {
69                 case 'w':
70                         global_flags.width = atoi(optarg);
71                         break;
72                 case 'h':
73                         global_flags.height = atoi(optarg);
74                         break;
75                 case 'r': {
76                         double num, den;
77                         if (sscanf(optarg, "%lf/%lf", &num, &den) == 2) {
78                                 global_flags.output_framerate = num / den;
79                         } else if (sscanf(optarg, "%lf", &num) == 1) {
80                                 global_flags.output_framerate = num;
81                         } else {
82                                 fprintf(stderr, "Invalid frame rate given (must be on the form N or N/M)\n");
83                                 exit(1);
84                         }
85                         break;
86                 }
87                 case OPTION_SLOW_DOWN_INPUT:
88                         global_flags.slow_down_input = true;
89                         break;
90                 case 'q':
91                         global_flags.interpolation_quality = atoi(optarg);
92                         global_flags.interpolation_quality_set = true;
93                         break;
94                 case 'd':
95                         global_flags.working_directory = optarg;
96                         break;
97                 case OPTION_HTTP_PORT:
98                         global_flags.http_port = atoi(optarg);
99                         break;
100                 case OPTION_TALLY_URL:
101                         global_flags.tally_url = optarg;
102                         break;
103                 case OPTION_CUE_POINT_PADDING:
104                         global_flags.cue_point_padding_seconds = atof(optarg);
105                         global_flags.cue_point_padding_set = true;
106                         break;
107                 case OPTION_HELP:
108                         usage();
109                         exit(0);
110                 default:
111                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
112                         fprintf(stderr, "\n");
113                         usage();
114                         exit(1);
115                 }
116         }
117
118         if (global_flags.interpolation_quality < 0 || global_flags.interpolation_quality > 4) {
119                 fprintf(stderr, "Interpolation quality must be 0, 1, 2, 3 or 4.\n");
120                 usage();
121                 exit(1);
122         }
123         if (global_flags.cue_point_padding_seconds < 0.0) {
124                 fprintf(stderr, "Cue point padding cannot be negative.\n");
125                 usage();
126                 exit(1);
127         }
128 }