1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong DID Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "codec_utils.h"
17 #include <securec.h>
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20
21 #define HDF_LOG_TAG codec_hdi_demo_utils
22 #define CMD_OPTION_MARK_OFFSET 0
23 #define CMD_OPTION_NAME_OFFSET 1
24
GetCodecName(CodecCmd * cmd)25 static int32_t GetCodecName(CodecCmd* cmd)
26 {
27 int32_t ret = HDF_SUCCESS;
28 if (strstr(cmd->codecName, "avc") || strstr(cmd->codecName, "AVC")) {
29 if (cmd->type == VIDEO_ENCODER) {
30 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, "codec.avc.hardware.encoder");
31 } else {
32 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, "codec.avc.hardware.decoder");
33 }
34 } else if (strstr(cmd->codecName, "hevc") || strstr(cmd->codecName, "HEVC")) {
35 if (cmd->type == VIDEO_ENCODER) {
36 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, "codec.hevc.hardware.encoder");
37 } else {
38 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, "codec.hevc.hardware.decoder");
39 }
40 } else {
41 memset_s(cmd->codecName, TYPE_NAME_LENGTH, 0, TYPE_NAME_LENGTH);
42 HDF_LOGE("%{public}s: not support coding codecName", __func__);
43 ret = HDF_FAILURE;
44 }
45 return ret;
46 }
47
ParseCmdOption(CodecCmd * cmd,const char * opt,const char * next)48 static int32_t ParseCmdOption(CodecCmd* cmd, const char *opt, const char *next)
49 {
50 int32_t ret = HDF_SUCCESS;
51 if (cmd == NULL || opt == NULL || next == NULL) {
52 return HDF_FAILURE;
53 }
54 switch (*opt) {
55 case 'i': {
56 int32_t len = strnlen(next, MAX_FILE_NAME_LENGTH);
57 if (len) {
58 strcpy_s(cmd->fileInput, MAX_FILE_NAME_LENGTH, next);
59 } else {
60 ret = HDF_FAILURE;
61 }
62 } break;
63 case 'o': {
64 int32_t len = strnlen(next, MAX_FILE_NAME_LENGTH);
65 if (len) {
66 strcpy_s(cmd->fileOutput, MAX_FILE_NAME_LENGTH, next);
67 } else {
68 ret = HDF_FAILURE;
69 }
70 } break;
71 case 'w': {
72 cmd->width = atoi(next);
73 } break;
74 case 'h': {
75 cmd->height = atoi(next);
76 } break;
77 case 't': {
78 int32_t len = strnlen(next, TYPE_NAME_LENGTH);
79 if (len) {
80 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, next);
81 ret = GetCodecName(cmd);
82 } else {
83 ret = HDF_FAILURE;
84 }
85 } break;
86 default:
87 break;
88 }
89 return ret;
90 }
91
ParseArguments(CodecCmd * cmd,int argc,char ** argv)92 int32_t ParseArguments(CodecCmd* cmd, int argc, char **argv)
93 {
94 int32_t optindex = 1;
95 int32_t ret = HDF_SUCCESS;
96
97 if ((argc <= 1) || (cmd == NULL))
98 return ret;
99
100 /* parse options */
101 while (optindex < argc) {
102 const char *opt = (const char*)argv[optindex++];
103 const char *next = (const char*)argv[optindex];
104 int32_t optMark = CMD_OPTION_MARK_OFFSET;
105 int32_t optName = CMD_OPTION_NAME_OFFSET;
106
107 if ((opt[optMark] == '-') && (opt[optName] != '\0')) {
108 optMark++;
109 optName++;
110 if ((opt[optMark] == '-') && (opt[optName] != '\0')) {
111 opt++;
112 }
113 if ((opt[optMark] == '-') && (opt[optName] == '\0')) {
114 ret = HDF_FAILURE;
115 break;
116 }
117
118 opt++;
119 if (ParseCmdOption(cmd, opt, next) == HDF_FAILURE) {
120 ret = HDF_FAILURE;
121 break;
122 }
123 optindex++;
124 }
125 }
126 return ret;
127 }