]> git.sesse.net Git - nageru/blobdiff - nageru/flags.cpp
On errors, abort() instead of exit(1); exit() in a multithreaded program just gives...
[nageru] / nageru / flags.cpp
index 9b3a9dac7f302de31f315154f85f0236b301545b..b025076c0639361fd74307a99af986582e089999 100644 (file)
@@ -63,8 +63,57 @@ enum LongOption {
        OPTION_10_BIT_INPUT,
        OPTION_10_BIT_OUTPUT,
        OPTION_INPUT_YCBCR_INTERPRETATION,
+       OPTION_MJPEG_EXPORT_CARDS,
 };
 
+map<unsigned, unsigned> parse_mjpeg_export_cards(char *optarg)
+{
+       map<unsigned, unsigned> ret;
+       if (optarg[0] == '\0') {
+               return ret;
+       }
+
+       unsigned stream_idx = 0;
+       char *start = optarg;
+       for ( ;; ) {
+               char *end = strchr(start, ',');
+               if (end != nullptr) {
+                       *end = '\0';
+               }
+
+               unsigned range_begin, range_end;
+               if (sscanf(start, "%u-%u", &range_begin, &range_end) != 2) {
+                       range_begin = range_end = atoi(start);
+               }
+               if (range_end < range_begin) {
+                       fprintf(stderr, "ERROR: Invalid range %u-%u in --mjpeg-export-cards=\n", range_begin, range_end);
+                       abort();
+               }
+               if (range_end >= unsigned(global_flags.num_cards)) {
+                       // There are situations where we could possibly want to
+                       // include FFmpeg inputs (CEF inputs are unlikely),
+                       // but they're not necessarily in 4:2:2 Y'CbCr, so it would
+                       // require more functionality the the JPEG encoder.
+                       fprintf(stderr, "ERROR: Asked for (zero-indexed) card %u in --mjpeg-export-cards=, but there are only %u cards\n",
+                               range_end, global_flags.num_cards);
+                       abort();
+               }
+               for (unsigned card_idx = range_begin; card_idx <= range_end; ++card_idx) {
+                       if (ret.count(card_idx)) {
+                               fprintf(stderr, "ERROR: Card %u was given twice in --mjpeg-export-cards=\n", card_idx);
+                               abort();
+                       }
+                       ret[card_idx] = stream_idx++;
+               }
+               if (end == nullptr) {
+                       break;
+               } else {
+                       start = end + 1;
+               }
+       }
+       return ret;
+}
+
 void usage(Program program)
 {
        if (program == PROGRAM_KAERU) {
@@ -160,6 +209,10 @@ void usage(Program program)
                fprintf(stderr, "                                  Y'CbCr coefficient standard of card CARD (default auto)\n");
                fprintf(stderr, "                                    auto is rec601 for SD, rec709 for HD, always limited\n");
                fprintf(stderr, "                                    limited means standard 0-240/0-235 input range (for 8-bit)\n");
+               fprintf(stderr, "      --mjpeg-export-cards=RANGE[,RANGE...]\n");
+               fprintf(stderr, "                                  export the given cards in MJPEG format to /multicam.mp4,\n");
+               fprintf(stderr, "                                    in the given order (ranges can be either single card indexes\n");
+               fprintf(stderr, "                                    or pairs like 1-3 for camera 1,2,3; default is all cards)\n");
        }
 }
 
@@ -225,10 +278,12 @@ void parse_flags(Program program, int argc, char * const argv[])
                { "10-bit-input", no_argument, 0, OPTION_10_BIT_INPUT },
                { "10-bit-output", no_argument, 0, OPTION_10_BIT_OUTPUT },
                { "input-ycbcr-interpretation", required_argument, 0, OPTION_INPUT_YCBCR_INTERPRETATION },
+               { "mjpeg-export-cards", required_argument, 0, OPTION_MJPEG_EXPORT_CARDS },
                { 0, 0, 0, 0 }
        };
        vector<string> theme_dirs;
        string output_ycbcr_coefficients = "auto";
+       bool card_to_mjpeg_stream_export_set = false;
        for ( ;; ) {
                int option_index = 0;
                int c = getopt_long(argc, argv, "c:t:I:r:v:m:M:w:h:", long_options, &option_index);
@@ -262,7 +317,7 @@ void parse_flags(Program program, int argc, char * const argv[])
                        char *ptr = strchr(optarg, ',');
                        if (ptr == nullptr) {
                                fprintf(stderr, "ERROR: Invalid argument '%s' to --map-signal (needs a signal and a card number, separated by comma)\n", optarg);
-                               exit(1);
+                               abort();
                        }
                        *ptr = '\0';
                        const int signal_num = atoi(optarg);
@@ -270,7 +325,7 @@ void parse_flags(Program program, int argc, char * const argv[])
                        if (global_flags.default_stream_mapping.count(signal_num)) {
                                fprintf(stderr, "ERROR: Signal %d already mapped to card %d\n",
                                        signal_num, global_flags.default_stream_mapping[signal_num]);
-                               exit(1);
+                               abort();
                        }
                        global_flags.default_stream_mapping[signal_num] = card_num;
                        break;
@@ -434,13 +489,13 @@ void parse_flags(Program program, int argc, char * const argv[])
                        char *ptr = strchr(optarg, ',');
                        if (ptr == nullptr) {
                                fprintf(stderr, "ERROR: Invalid argument '%s' to --input-ycbcr-interpretation (needs a card and an interpretation, separated by comma)\n", optarg);
-                               exit(1);
+                               abort();
                        }
                        *ptr = '\0';
                        const int card_num = atoi(optarg);
                        if (card_num < 0 || card_num >= MAX_VIDEO_CARDS) {
                                fprintf(stderr, "ERROR: Invalid card number %d\n", card_num);
-                               exit(1);
+                               abort();
                        }
 
                        YCbCrInterpretation interpretation;
@@ -455,7 +510,7 @@ void parse_flags(Program program, int argc, char * const argv[])
                                        interpretation.full_range = false;
                                } else {
                                        fprintf(stderr, "ERROR: Invalid Y'CbCr range '%s' (must be “full” or “limited”)\n", range);
-                                       exit(1);
+                                       abort();
                                }
                        }
 
@@ -469,11 +524,11 @@ void parse_flags(Program program, int argc, char * const argv[])
                                interpretation.ycbcr_coefficients_auto = true;
                                if (interpretation.full_range) {
                                        fprintf(stderr, "ERROR: Cannot use “auto” Y'CbCr coefficients with full range\n");
-                                       exit(1);
+                                       abort();
                                }
                        } else {
                                fprintf(stderr, "ERROR: Invalid Y'CbCr coefficients '%s' (must be “rec601”, “rec709” or “auto”)\n", interpretation_str);
-                               exit(1);
+                               abort();
                        }
                        global_flags.ycbcr_interpretation[card_num] = interpretation;
                        break;
@@ -481,6 +536,15 @@ void parse_flags(Program program, int argc, char * const argv[])
                case OPTION_FULLSCREEN:
                        global_flags.fullscreen = true;
                        break;
+               case OPTION_MJPEG_EXPORT_CARDS: {
+                       if (card_to_mjpeg_stream_export_set) {
+                               fprintf(stderr, "ERROR: --mjpeg-export-cards given twice\n");
+                               abort();
+                       }
+                       global_flags.card_to_mjpeg_stream_export = parse_mjpeg_export_cards(optarg);    
+                       card_to_mjpeg_stream_export_set = true;
+                       break;
+               }
                case OPTION_HELP:
                        usage(program);
                        exit(0);
@@ -488,28 +552,28 @@ void parse_flags(Program program, int argc, char * const argv[])
                        fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
                        fprintf(stderr, "\n");
                        usage(program);
-                       exit(1);
+                       abort();
                }
        }
 
        if (global_flags.uncompressed_video_to_http &&
            global_flags.x264_video_to_http) {
                fprintf(stderr, "ERROR: --http-uncompressed-video and --http-x264-video are mutually incompatible\n");
-               exit(1);
+               abort();
        }
        if (global_flags.num_cards <= 0) {
                fprintf(stderr, "ERROR: --num-cards must be at least 1\n");
-               exit(1);
+               abort();
        }
        if (global_flags.output_card < -1 ||
            global_flags.output_card >= global_flags.num_cards) {
                fprintf(stderr, "ERROR: --output-card points to a nonexistant card\n");
-               exit(1);
+               abort();
        }
        if (!global_flags.transcode_audio && global_flags.stream_audio_codec_name.empty()) {
                fprintf(stderr, "ERROR: If not transcoding audio, you must specify ahead-of-time what audio codec is in use\n");
                fprintf(stderr, "       (using --http-audio-codec).\n");
-               exit(1);
+               abort();
        }
        if (global_flags.x264_speedcontrol) {
                if (!global_flags.x264_preset.empty() && global_flags.x264_preset != "faster") {
@@ -530,14 +594,14 @@ void parse_flags(Program program, int argc, char * const argv[])
        if (global_flags.width <= 0 || (global_flags.width % 8) != 0 ||
            global_flags.height <= 0 || (global_flags.height % 8) != 0) {
                fprintf(stderr, "ERROR: --width and --height must be positive integers divisible by 8\n");
-               exit(1);
+               abort();
        }
 
        for (pair<int, int> mapping : global_flags.default_stream_mapping) {
                if (mapping.second >= global_flags.num_cards) {
                        fprintf(stderr, "ERROR: Signal %d mapped to card %d, which doesn't exist (try adjusting --num-cards)\n",
                                mapping.first, mapping.second);
-                       exit(1);
+                       abort();
                }
        }
 
@@ -569,22 +633,22 @@ void parse_flags(Program program, int argc, char * const argv[])
                global_flags.ycbcr_auto_coefficients = false;
        } else {
                fprintf(stderr, "ERROR: --output-ycbcr-coefficients must be “rec601”, “rec709” or “auto”\n");
-               exit(1);
+               abort();
        }
 
        if (global_flags.output_buffer_frames < 0.0f) {
                // Actually, even zero probably won't make sense; there is some internal
                // delay to the card.
                fprintf(stderr, "ERROR: --output-buffer-frames can't be negative.\n");
-               exit(1);
+               abort();
        }
        if (global_flags.output_slop_frames < 0.0f) {
                fprintf(stderr, "ERROR: --output-slop-frames can't be negative.\n");
-               exit(1);
+               abort();
        }
        if (global_flags.max_input_queue_frames < 1) {
                fprintf(stderr, "ERROR: --max-input-queue-frames must be at least 1.\n");
-               exit(1);
+               abort();
        }
        if (global_flags.max_input_queue_frames > 10) {
                fprintf(stderr, "WARNING: --max-input-queue-frames has little effect over 10.\n");
@@ -593,7 +657,7 @@ void parse_flags(Program program, int argc, char * const argv[])
        if (!isinf(global_flags.x264_crf)) {  // CRF mode is selected.
                if (global_flags.x264_bitrate != -1) {
                        fprintf(stderr, "ERROR: --x264-bitrate and --x264-crf are mutually incompatible.\n");
-                       exit(1);
+                       abort();
                }
                if (global_flags.x264_vbv_max_bitrate != -1 && global_flags.x264_vbv_buffer_size != -1) {
                        fprintf(stderr, "WARNING: VBV settings are ignored with --x264-crf.\n");
@@ -601,4 +665,11 @@ void parse_flags(Program program, int argc, char * const argv[])
        } else if (global_flags.x264_bitrate == -1) {
                global_flags.x264_bitrate = DEFAULT_X264_OUTPUT_BIT_RATE;
        }
+
+       if (!card_to_mjpeg_stream_export_set) {
+               // Fill in the default mapping (export all cards, in order).
+               for (unsigned card_idx = 0; card_idx < unsigned(global_flags.num_cards); ++card_idx) {
+                       global_flags.card_to_mjpeg_stream_export[card_idx] = card_idx;
+               }
+       }
 }