1 /*
2 * Copyright (c) 2022-2023 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 #define STRTOL_BASE 10
25
GetCodecName(CodecCmd * cmd)26 static int32_t GetCodecName(CodecCmd* cmd)
27 {
28 int32_t codecNum = 0;
29 CodecTypeAndName *codecs;
30 CodecTypeAndName encoders[] = {
31 {{"avc", "AVC"}, CODEC_NAME_AVC_HW_ENCODER, MEDIA_MIMETYPE_VIDEO_AVC},
32 {{"hevc", "HEVC"}, CODEC_NAME_HEVC_HW_ENCODER, MEDIA_MIMETYPE_VIDEO_HEVC},
33 {{"vp9", "VP9"}, CODEC_NAME_VP9_HW_ENCODER, MEDIA_MIMETYPE_INVALID}, // MIMETYPE NOT DEFINED YET
34 {{"vp8", "VP8"}, CODEC_NAME_VP8_HW_ENCODER, MEDIA_MIMETYPE_INVALID}, // MIMETYPE NOT DEFINED YET
35 {{"mpeg4", "MPEG4"}, CODEC_NAME_MPEG4_HW_ENCODER, MEDIA_MIMETYPE_VIDEO_MPEG4}
36 };
37 CodecTypeAndName decoders[] = {
38 {{"avc", "AVC"}, CODEC_NAME_AVC_HW_DECODER, MEDIA_MIMETYPE_VIDEO_AVC},
39 {{"hevc", "HEVC"}, CODEC_NAME_HEVC_HW_DECODER, MEDIA_MIMETYPE_VIDEO_HEVC},
40 {{"vp9", "VP9"}, CODEC_NAME_VP9_HW_DECODER, MEDIA_MIMETYPE_INVALID}, // MIMETYPE NOT DEFINED YET
41 {{"vp8", "VP8"}, CODEC_NAME_VP8_HW_DECODER, MEDIA_MIMETYPE_INVALID}, // MIMETYPE NOT DEFINED YET
42 {{"mpeg4", "MPEG4"}, CODEC_NAME_MPEG4_HW_DECODER, MEDIA_MIMETYPE_VIDEO_MPEG4}
43 };
44
45 if (cmd->type == VIDEO_ENCODER) {
46 codecNum = sizeof(encoders) / sizeof(CodecTypeAndName);
47 codecs = encoders;
48 } else if (cmd->type == VIDEO_DECODER) {
49 codecNum = sizeof(decoders) / sizeof(CodecTypeAndName);
50 codecs = decoders;
51 }
52
53 for (int32_t i = 0; i < codecNum; i++) {
54 if (strstr(cmd->codecName, codecs[i].codecType[0]) || strstr(cmd->codecName, codecs[i].codecType[1])) {
55 int32_t ret = strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, codecs[i].codecName);
56 if (ret != EOK) {
57 HDF_LOGE("%{public}s, failed to strcpy_s codecName. ret:%{public}d", __func__, ret);
58 return HDF_FAILURE;
59 }
60 cmd->mime = codecs[i].mimeType;
61 return HDF_SUCCESS;
62 }
63 }
64
65 HDF_LOGE("%{public}s: not support coding codecName", __func__);
66 return HDF_FAILURE;
67 }
68
ParseCmdOption(CodecCmd * cmd,const char * opt,const char * next)69 static int32_t ParseCmdOption(CodecCmd* cmd, const char *opt, const char *next)
70 {
71 int32_t ret = HDF_SUCCESS;
72 if (cmd == NULL || opt == NULL || next == NULL) {
73 return HDF_FAILURE;
74 }
75 switch (*opt) {
76 case 'i': {
77 int32_t len = strnlen(next, MAX_FILE_NAME_LENGTH);
78 if (len) {
79 strcpy_s(cmd->fileInput, MAX_FILE_NAME_LENGTH, next);
80 } else {
81 ret = HDF_FAILURE;
82 }
83 } break;
84 case 'o': {
85 int32_t len = strnlen(next, MAX_FILE_NAME_LENGTH);
86 if (len) {
87 strcpy_s(cmd->fileOutput, MAX_FILE_NAME_LENGTH, next);
88 } else {
89 ret = HDF_FAILURE;
90 }
91 } break;
92 case 'w': {
93 cmd->width = (int32_t)strtol(next, NULL, STRTOL_BASE);
94 } break;
95 case 'h': {
96 cmd->height = (int32_t)strtol(next, NULL, STRTOL_BASE);
97 } break;
98 case 't': {
99 int32_t len = strnlen(next, TYPE_NAME_LENGTH);
100 if (len) {
101 strcpy_s(cmd->codecName, TYPE_NAME_LENGTH, next);
102 ret = GetCodecName(cmd);
103 } else {
104 ret = HDF_FAILURE;
105 }
106 } break;
107 case 'f': {
108 cmd->fps = (int32_t)strtol(next, NULL, STRTOL_BASE);
109 } break;
110 case 'p': {
111 cmd->pixFmt = (int32_t)strtol(next, NULL, STRTOL_BASE);
112 } break;
113 default:
114 break;
115 }
116 return ret;
117 }
118
ParseArguments(CodecCmd * cmd,int argc,char ** argv)119 int32_t ParseArguments(CodecCmd* cmd, int argc, char **argv)
120 {
121 int32_t optindex = 1;
122 int32_t ret = HDF_SUCCESS;
123
124 if ((argc <= 1) || (cmd == NULL)) {
125 return ret;
126 }
127
128 /* parse options */
129 while (optindex < argc) {
130 const char *opt = (const char*)argv[optindex++];
131 const char *next = (const char*)argv[optindex];
132 int32_t optMark = CMD_OPTION_MARK_OFFSET;
133 int32_t optName = CMD_OPTION_NAME_OFFSET;
134
135 if ((opt[optMark] == '-') && (opt[optName] != '\0')) {
136 optMark++;
137 optName++;
138 if ((opt[optMark] == '-') && (opt[optName] != '\0')) {
139 opt++;
140 }
141 if ((opt[optMark] == '-') && (opt[optName] == '\0')) {
142 ret = HDF_FAILURE;
143 break;
144 }
145
146 opt++;
147 if (ParseCmdOption(cmd, opt, next) == HDF_FAILURE) {
148 ret = HDF_FAILURE;
149 break;
150 }
151 optindex++;
152 }
153 }
154 return ret;
155 }
156
FreeParams(Param * params,int32_t paramCnt)157 void FreeParams(Param *params, int32_t paramCnt)
158 {
159 if (params == NULL || paramCnt <= 0) {
160 HDF_LOGE("%{public}s: params is null or invalid count!", __func__);
161 return;
162 }
163 for (int32_t j = 0; j < paramCnt; j++) {
164 if (params[j].val != NULL && params[j].size > 0) {
165 OsalMemFree(params[j].val);
166 params[j].val = NULL;
167 }
168 }
169 OsalMemFree(params);
170 }
171
172