1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright 2023 Shenzhen Kaihong DID Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci * 		http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include "command_parse.h"
16094332d3Sopenharmony_ci#include <getopt.h>
17094332d3Sopenharmony_ci#include <iostream>
18094332d3Sopenharmony_cienum class MyOptIndex {
19094332d3Sopenharmony_ci    OPT_INDEX_UNKONWN = 0,
20094332d3Sopenharmony_ci    OPT_INDEX_BUFFER_HANDLE,
21094332d3Sopenharmony_ci    OPT_INDEX_DMA_BUFFER,
22094332d3Sopenharmony_ci    OPT_INDEX_HEVC,
23094332d3Sopenharmony_ci    OPT_INDEX_HELP,
24094332d3Sopenharmony_ci    OPT_INDEX_HEIGHT = 'h',
25094332d3Sopenharmony_ci    OPT_INDEX_INPUT = 'i',
26094332d3Sopenharmony_ci    OPT_INDEX_OUTPUT = 'o',
27094332d3Sopenharmony_ci    OPT_INDEX_WIDTH = 'w'
28094332d3Sopenharmony_ci};
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_cistatic struct option g_longOptions[] = {
31094332d3Sopenharmony_ci    {"width", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_WIDTH)},
32094332d3Sopenharmony_ci    {"height", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEIGHT)},
33094332d3Sopenharmony_ci    {"in", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_INPUT)},
34094332d3Sopenharmony_ci    {"out", required_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_OUTPUT)},
35094332d3Sopenharmony_ci    {"nocopy", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_BUFFER_HANDLE)},
36094332d3Sopenharmony_ci    {"dma", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_DMA_BUFFER)},
37094332d3Sopenharmony_ci    {"HEVC", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HEVC)},
38094332d3Sopenharmony_ci    {"help", no_argument, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_HELP)},
39094332d3Sopenharmony_ci    {nullptr, 0, nullptr, static_cast<int>(MyOptIndex::OPT_INDEX_UNKONWN)}
40094332d3Sopenharmony_ci};
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_cibool CommandParse::Parse(int argc, char *argv[], CommandOpt &opt)
43094332d3Sopenharmony_ci{
44094332d3Sopenharmony_ci    while (1) {
45094332d3Sopenharmony_ci        int optionIndex = 0;
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci        int c = getopt_long(argc, argv, "i:o:w:h:", g_longOptions, &optionIndex);
48094332d3Sopenharmony_ci        if (c == -1) {
49094332d3Sopenharmony_ci            break;
50094332d3Sopenharmony_ci        }
51094332d3Sopenharmony_ci        MyOptIndex index = (MyOptIndex)c;
52094332d3Sopenharmony_ci        switch (index) {
53094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_BUFFER_HANDLE:
54094332d3Sopenharmony_ci                opt.useBufferHandle = true;
55094332d3Sopenharmony_ci                break;
56094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_DMA_BUFFER:
57094332d3Sopenharmony_ci                opt.useDMABuffer = true;
58094332d3Sopenharmony_ci                break;
59094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HEVC:
60094332d3Sopenharmony_ci                opt.codec = codecMime::HEVC;
61094332d3Sopenharmony_ci                break;
62094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HELP:
63094332d3Sopenharmony_ci                ShowUsage();
64094332d3Sopenharmony_ci                break;
65094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_INPUT:
66094332d3Sopenharmony_ci                opt.fileInput = optarg;
67094332d3Sopenharmony_ci                break;
68094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_OUTPUT:
69094332d3Sopenharmony_ci                opt.fileOutput = optarg;
70094332d3Sopenharmony_ci                break;
71094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_WIDTH:
72094332d3Sopenharmony_ci                opt.width = atoi(optarg);
73094332d3Sopenharmony_ci                break;
74094332d3Sopenharmony_ci            case MyOptIndex::OPT_INDEX_HEIGHT:
75094332d3Sopenharmony_ci                opt.height = atoi(optarg);
76094332d3Sopenharmony_ci                break;
77094332d3Sopenharmony_ci            default:
78094332d3Sopenharmony_ci                ShowUsage();
79094332d3Sopenharmony_ci                break;
80094332d3Sopenharmony_ci        }
81094332d3Sopenharmony_ci    }
82094332d3Sopenharmony_ci    if (opt.fileInput.empty() || opt.fileOutput.empty() || opt.width == 0 || opt.height == 0) {
83094332d3Sopenharmony_ci        return false;
84094332d3Sopenharmony_ci    }
85094332d3Sopenharmony_ci    return true;
86094332d3Sopenharmony_ci}
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_civoid CommandParse::ShowUsage()
89094332d3Sopenharmony_ci{
90094332d3Sopenharmony_ci    std::cout << "Options:" << std::endl;
91094332d3Sopenharmony_ci    std::cout << " -w, --width=width          The video width." << std::endl;
92094332d3Sopenharmony_ci    std::cout << " -h, --height=height        The video height." << std::endl;
93094332d3Sopenharmony_ci    std::cout << " -o, --out=FILE             The file name for output file." << std::endl;
94094332d3Sopenharmony_ci    std::cout << " -i, --in=FILE              The file name for input file." << std::endl;
95094332d3Sopenharmony_ci    std::cout << " --HEVC                     HEVC decode or HEVC encode, AVC for default." << std::endl;
96094332d3Sopenharmony_ci    std::cout << " --nocopy                   Support BufferHandle." << std::endl;
97094332d3Sopenharmony_ci    std::cout << " --dma                      Support dma Buffer." << std::endl;
98094332d3Sopenharmony_ci    std::cout << " --help                     The help info." << std::endl;
99094332d3Sopenharmony_ci}
100