<commit_before>#include "factory.h"
#include <chrono>
#include <thread>

void synopsis()
{
    std::cout << "save_frames"
              << "  [  "
              << std::endl << "                "
              << " help "
              << std::endl << "                "
              << " epiphan   xvid | h265   sdi | dvi   "
              << " [ <framerate>   [ <x> <y>  <width> <height> ]   ]"
              << " # optimal: 20 600 185 678 688"
              << std::endl << "                "
              << " file   </file/path>   xvid | h265 "
              << std::endl << "                "
              << " chess   xvid | h265   <width>   <height> "
              << std::endl << "            "
              << " ]"
              << std::endl;
}

enum TestMode { Epiphan, File, Chessboard };

enum TestMode test_mode;
enum gg::Target codec;
std::string codec_string, filetype;
int width, height, square_size;
float fps;
int duration; // min
int num_frames;
int i = 0;
VideoFrame_BGRA frame;
cv::Mat image;
cv::VideoCapture cap;
enum gg::Device port;
std::string port_string;
int sleep_duration;
IVideoSource * epiphan = NULL;
int roi_x, roi_y;

inline
void timer_start(std::chrono::high_resolution_clock::time_point & start)
{
    start = std::chrono::high_resolution_clock::now();
}

inline
float timer_end(const std::chrono::high_resolution_clock::time_point & start)
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(
                            std::chrono::high_resolution_clock::now() - start
                                                                ).count();
}

void init(int argc, char ** argv)
{
    std::vector<std::string> args;
    for (int i = 0; i < argc; i++)
        args.push_back(std::string(argv[i]));

    if (args[1]=="help")
    {
        synopsis(); exit(0);
    }

    if (args[1]=="epiphan")
    {
        test_mode = TestMode::Epiphan;

        if (argc < 4)
        {
            synopsis(); exit(-1);
        }

        codec_string = args[2];
        port_string = args[3];

        if (port_string == "sdi")
            port = gg::Device::DVI2PCIeDuo_SDI;
        else if (port_string == "dvi")
            port = gg::Device::DVI2PCIeDuo_DVI;
        else
        {
            std::cerr << "Port " << port_string << " not recognised" << std::endl;
            synopsis();
            exit(-1);
        }

        epiphan = gg::Factory::connect(port);
        fps = 60;
        duration = 1; // min

        // optimal for Storz 27020 AA straight
        if (argc >= 5)
        {
            fps = atof(argv[4]);
            if (argc >= 9)
            {
                roi_x = atoi(argv[5]);
                roi_y = atoi(argv[6]);
                width = atoi(argv[7]);
                height = atoi(argv[8]);
                epiphan->set_sub_frame(roi_x, roi_y, width, height);
            }
        }

        num_frames = duration * 60 * fps;
        sleep_duration = (int)(1000/fps); // ms
    }

    else if (args[1]=="file")
    {
        test_mode = TestMode::File;

        if (argc < 4)
        {
            synopsis(); exit(-1);
        }

        cap.open(args[2]);
        if (not cap.isOpened())
        {
            std::cerr << "File " << args[2]
                      << " could not be opened"
                      << std::endl;
            exit(-1);
        }

        codec_string = args[3];
        num_frames = cap.get(CV_CAP_PROP_FRAME_COUNT);
        fps = cap.get(CV_CAP_PROP_FPS);
        duration = (num_frames / fps) / 60;
    }

    else if (args[1]=="chess")
    {
        test_mode = TestMode::Chessboard;

        if (argc < 5)
        {
            synopsis(); exit(-1);
        }

        codec_string = args[2];
        width = atoi(argv[3]); height = atoi(argv[4]);
        /* square size purposefully odd,
         * to test robustness when odd frame
         * width and/or height provided, whereas
         * H265 requires even
         */
        square_size = 61;
        image = cv::Mat(height * square_size, width * square_size, CV_8UC4);

        fps = 60;
        duration = 1; // min
        num_frames = duration * 60 * fps;
    }

    else
    {
        synopsis(); exit(-1);
    }

    // health checks
    if (codec_string == "xvid")
    {
        codec = gg::Target::File_XviD;
        filetype = "avi";
    }
    else if (codec_string == "h265")
    {
        codec = gg::Target::File_H265;
        filetype = "mp4";
    }
    else
    {
        std::cerr << "Codec " << codec_string << " not recognised" << std::endl;
        synopsis();
        exit(-1);
    }
}

std::string which_file()
{
    std::string filename;
    filename.append(std::to_string(duration))
            .append("min_");
    switch(test_mode)
    {
    case TestMode::Epiphan:
        filename.append("epiphan"); break;
    case TestMode::File:
        filename.append("from_file"); break;
    case TestMode::Chessboard:
        filename.append("colour_chessboard"); break;
    default:
        std::cerr << "Test mode not set" << std::endl;
        exit(-1);
    }
    filename.append("_")
            .append(codec_string)
            .append(".")
            .append(filetype);
    return filename;
}

void get_frame(VideoFrame_BGRA & frame)
{
    // Chessboard params
    float elapsed, remaining;
    int red, green, blue, alpha, min_intensity;
    switch(test_mode)<commit_msg>Issue #21: referencing Epiphan port in produced video filename<commit_after>#include "factory.h"
#include <chrono>
#include <thread>

void synopsis()
{
    std::cout << "save_frames"
              << "  [  "
              << std::endl << "                "
              << " help "
              << std::endl << "                "
              << " epiphan   xvid | h265   sdi | dvi   "
              << " [ <framerate>   [ <x> <y>  <width> <height> ]   ]"
              << " # optimal: 20 600 185 678 688"
              << std::endl << "                "
              << " file   </file/path>   xvid | h265 "
              << std::endl << "                "
              << " chess   xvid | h265   <width>   <height> "
              << std::endl << "            "
              << " ]"
              << std::endl;
}

enum TestMode { Epiphan, File, Chessboard };

enum TestMode test_mode;
enum gg::Target codec;
std::string codec_string, filetype;
int width, height, square_size;
float fps;
int duration; // min
int num_frames;
int i = 0;
VideoFrame_BGRA frame;
cv::Mat image;
cv::VideoCapture cap;
enum gg::Device port;
std::string port_string;
int sleep_duration;
IVideoSource * epiphan = NULL;
int roi_x, roi_y;

inline
void timer_start(std::chrono::high_resolution_clock::time_point & start)
{
    start = std::chrono::high_resolution_clock::now();
}

inline
float timer_end(const std::chrono::high_resolution_clock::time_point & start)
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(
                            std::chrono::high_resolution_clock::now() - start
                                                                ).count();
}

void init(int argc, char ** argv)
{
    std::vector<std::string> args;
    for (int i = 0; i < argc; i++)
        args.push_back(std::string(argv[i]));

    if (args[1]=="help")
    {
        synopsis(); exit(0);
    }

    if (args[1]=="epiphan")
    {
        test_mode = TestMode::Epiphan;

        if (argc < 4)
        {
            synopsis(); exit(-1);
        }

        codec_string = args[2];
        port_string = args[3];

        if (port_string == "sdi")
            port = gg::Device::DVI2PCIeDuo_SDI;
        else if (port_string == "dvi")
            port = gg::Device::DVI2PCIeDuo_DVI;
        else
        {
            std::cerr << "Port " << port_string << " not recognised" << std::endl;
            synopsis();
            exit(-1);
        }

        epiphan = gg::Factory::connect(port);
        fps = 60;
        duration = 1; // min

        // optimal for Storz 27020 AA straight
        if (argc >= 5)
        {
            fps = atof(argv[4]);
            if (argc >= 9)
            {
                roi_x = atoi(argv[5]);
                roi_y = atoi(argv[6]);
                width = atoi(argv[7]);
                height = atoi(argv[8]);
                epiphan->set_sub_frame(roi_x, roi_y, width, height);
            }
        }

        num_frames = duration * 60 * fps;
        sleep_duration = (int)(1000/fps); // ms
    }

    else if (args[1]=="file")
    {
        test_mode = TestMode::File;

        if (argc < 4)
        {
            synopsis(); exit(-1);
        }

        cap.open(args[2]);
        if (not cap.isOpened())
        {
            std::cerr << "File " << args[2]
                      << " could not be opened"
                      << std::endl;
            exit(-1);
        }

        codec_string = args[3];
        num_frames = cap.get(CV_CAP_PROP_FRAME_COUNT);
        fps = cap.get(CV_CAP_PROP_FPS);
        duration = (num_frames / fps) / 60;
    }

    else if (args[1]=="chess")
    {
        test_mode = TestMode::Chessboard;

        if (argc < 5)
        {
            synopsis(); exit(-1);
        }

        codec_string = args[2];
        width = atoi(argv[3]); height = atoi(argv[4]);
        /* square size purposefully odd,
         * to test robustness when odd frame
         * width and/or height provided, whereas
         * H265 requires even
         */
        square_size = 61;
        image = cv::Mat(height * square_size, width * square_size, CV_8UC4);

        fps = 60;
        duration = 1; // min
        num_frames = duration * 60 * fps;
    }

    else
    {
        synopsis(); exit(-1);
    }

    // health checks
    if (codec_string == "xvid")
    {
        codec = gg::Target::File_XviD;
        filetype = "avi";
    }
    else if (codec_string == "h265")
    {
        codec = gg::Target::File_H265;
        filetype = "mp4";
    }
    else
    {
        std::cerr << "Codec " << codec_string << " not recognised" << std::endl;
        synopsis();
        exit(-1);
    }
}

std::string which_file()
{
    std::string filename;
    filename.append(std::to_string(duration))
            .append("min_");
    switch(test_mode)
    {
    case TestMode::Epiphan:
        filename.append("epiphan_")
                .append(port_string); break;
    case TestMode::File:
        filename.append("from_file"); break;
    case TestMode::Chessboard:
        filename.append("colour_chessboard"); break;
    default:
        std::cerr << "Test mode not set" << std::endl;
        exit(-1);
    }
    filename.append("_")
            .append(codec_string)
            .append(".")
            .append(filetype);
    return filename;
}

void get_frame(VideoFrame_BGRA & frame)
{
    // Chessboard params
    float elapsed, remaining;
    int red, green, blue, alpha, min_intensity;
    switch(test_mode)
{'fraction_non_alphanumeric': 0.1433840479483633, 'fraction_numerical': 0.017335177501152606, 'mean_word_length': 1.6485958485958485, 'pattern_counts': {'":': 0, '<': 171, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1972695', 'n_tokens_mistral': 3631, 'n_tokens_neox': 3443, 'n_words': 724}