2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __MEDIA_CODEC_INI_C__
18 #define __MEDIA_CODEC_INI_C__
23 #include <glib/gstdio.h>
26 #include <iniparser.h>
27 #include <media_codec_ini.h>
28 #include <media_codec_port.h>
30 #define DEFAULT_VALUE ""
32 #define DEFAULT_HW_DECODER_NAME ""
33 #define DEFAULT_HW_DECODER_MIME ""
34 #define DEFAULT_HW_DECODER_FORMAT ""
36 #define DEFAULT_HW_ENCODER_NAME ""
37 #define DEFAULT_HW_ENCODER_MIME ""
38 #define DEFAULT_HW_ENCODER_FORMAT ""
40 #define DEFAULT_SW_DECODER_NAME ""
41 #define DEFAULT_SW_DECODER_MIME ""
42 #define DEFAULT_SW_DECODER_FORMAT ""
44 #define DEFAULT_SW_ENCODER_NAME ""
45 #define DEFAULT_SW_ENCODER_MIME ""
46 #define DEFAULT_SW_ENCODER_FORMAT ""
49 #define CNAME_SIZE 512
51 static codec_list_t general_codec_list[] = {
52 {"h261", MEDIACODEC_H261},
53 {"h263", MEDIACODEC_H263},
54 {"h264", MEDIACODEC_H264},
55 {"mjpeg", MEDIACODEC_MJPEG},
56 {"mpeg1", MEDIACODEC_MPEG1},
57 {"mpeg2", MEDIACODEC_MPEG2},
58 {"mpeg4", MEDIACODEC_MPEG4},
59 {"hevc", MEDIACODEC_HEVC},
60 {"vp8", MEDIACODEC_VP8},
61 {"vp9", MEDIACODEC_VP9},
62 {"vc1", MEDIACODEC_VC1},
63 {"aac_lc", MEDIACODEC_AAC_LC},
64 {"aac_he", MEDIACODEC_AAC_HE},
65 {"aac_he_ps", MEDIACODEC_AAC_HE_PS},
66 {"mp3", MEDIACODEC_MP3},
67 {"amr_nb", MEDIACODEC_AMR_NB},
68 {"amr_wb", MEDIACODEC_AMR_WB},
69 {"vorbis", MEDIACODEC_VORBIS},
70 {"flac", MEDIACODEC_FLAC},
71 {"wmav1", MEDIACODEC_WMAV1},
72 {"wmav2", MEDIACODEC_WMAV2},
73 {"wmapro", MEDIACODEC_WMAPRO},
76 /* internal functions, macros here */
77 #ifdef MEDIA_CODEC_DEFAULT_INI
78 static gboolean _generate_default_ini(void);
81 static void _mc_ini_check_ini_status(void);
84 #define MEDIA_CODEC_INI_GET_STRING(x_dict, x_item, x_ini, x_default) \
86 const char *str = iniparser_getstring(x_dict, x_ini, x_default); \
89 (strlen(str) > 0) && \
90 (strlen(str) < MEDIA_CODEC_INI_MAX_STRLEN)) \
91 strncpy(x_item, str, MEDIA_CODEC_INI_MAX_STRLEN - 1); \
93 strncpy(x_item, x_default, MEDIA_CODEC_INI_MAX_STRLEN - 1); \
96 #define MEDIA_CODEC_INI_GET_STRING_FROM_LIST(x_dict, x_list, x_ini, x_default) \
99 char *usr_ptr = NULL; \
101 const char *delimiters = " ,"; \
102 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
103 MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
104 token = strtok_r(temp_arr, delimiters, &usr_ptr); \
107 strncpy(x_list.name, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
108 else if (index == 1) \
109 strncpy(x_list.mime, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
110 else if (index == 2) \
111 strncpy(x_list.format, token, MEDIA_CODEC_INI_MAX_STRLEN-1);\
113 token = strtok_r(NULL, delimiters, &usr_ptr); \
117 #define MEDIA_CODEC_INI_GET_COLOR(x_dict, x_item, x_ini, x_default) \
119 const char *str = iniparser_getstring(x_dict, x_ini, x_default); \
122 (strlen(str) > 0) && \
123 (strlen(str) < MEDIA_CODEC_INI_MAX_STRLEN)) \
124 x_item = (guint) strtoul(str, NULL, 16); \
126 x_item = (guint) strtoul(x_default, NULL, 16); \
129 /* x_ini is the list of index to set TRUE at x_list[index] */
130 #define MEDIA_CODEC_INI_GET_BOOLEAN_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
133 const char *delimiters = " ,"; \
134 char *usr_ptr = NULL; \
135 char *token = NULL; \
136 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
137 MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
138 token = strtok_r(temp_arr, delimiters, &usr_ptr); \
140 index = atoi(token); \
141 if (index < 0 || index > x_list_max -1) \
142 LOGW("%d is not valid index\n", index); \
144 x_list[index] = TRUE; \
145 token = strtok_r(NULL, delimiters, &usr_ptr); \
149 /* x_ini is the list of value to be set at x_list[index] */
150 #define MEDIA_CODEC_INI_GET_INT_FROM_LIST(x_dict, x_list, x_list_max, x_ini, x_default) \
154 const char *delimiters = " ,"; \
155 char *usr_ptr = NULL; \
156 char *token = NULL; \
157 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
158 MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
159 token = strtok_r(temp_arr, delimiters, &usr_ptr); \
161 if (index > x_list_max -1) {\
162 LOGE("%d is not valid index\n", index); \
166 value = atoi(token); \
167 x_list[index] = value; \
170 token = strtok_r(NULL, delimiters, &usr_ptr); \
174 #define MEDIA_CODEC_GET_DEFAULT_LIST(x_list, x_default) \
176 strncpy(x_list, x_default, MEDIA_CODEC_INI_MAX_STRLEN - 1);\
178 #define MEDIA_CODEC_PRINT_LIST(x_list, x_message) \
180 codec_info_t codec_list = x_list;\
181 LOGW("%s =", x_message);\
182 LOGW("%s %s %s\n", codec_list.name, codec_list.mime, codec_list.format);\
185 media_format_mimetype_e _mc_convert_media_format_str_to_int(char *sformat)
188 media_format_mimetype_e iformat = MEDIA_FORMAT_I420;
189 if (!strcmp(sformat, "I420")) {
190 iformat = MEDIA_FORMAT_I420;
192 } else if (!strcmp(sformat, "NV12")) {
193 iformat = MEDIA_FORMAT_NV12;
195 } else if (!strcmp(sformat, "NV12T")) {
196 iformat = MEDIA_FORMAT_NV12T;
198 } else if (!strcmp(sformat, "YV12")) {
199 iformat = MEDIA_FORMAT_YV12;
201 } else if (!strcmp(sformat, "NV21")) {
202 iformat = MEDIA_FORMAT_NV21;
204 } else if (!strcmp(sformat, "NV16")) {
205 iformat = MEDIA_FORMAT_NV16;
206 } else if (!strcmp(sformat, "YUYV")) {
207 iformat = MEDIA_FORMAT_YUYV;
209 } else if (!strcmp(sformat, "UYVY")) {
210 iformat = MEDIA_FORMAT_UYVY;
212 } else if (!strcmp(sformat, "422P")) {
213 iformat = MEDIA_FORMAT_422P;
215 } else if (!strcmp(sformat, "RGB565")) {
216 iformat = MEDIA_FORMAT_RGB565;
218 } else if (!strcmp(sformat, "RGB888")) {
219 iformat = MEDIA_FORMAT_RGB888;
221 } else if (!strcmp(sformat, "RGBA")) {
222 iformat = MEDIA_FORMAT_RGBA;
224 } else if (!strcmp(sformat, "ARGB")) {
225 iformat = MEDIA_FORMAT_ARGB;
227 } else if (!strcmp(sformat, "PCM")) {
228 iformat = MEDIA_FORMAT_PCM;
230 } else if (!strcmp(sformat, "PCM_F32LE")) {
231 iformat = MEDIA_FORMAT_PCM_F32LE;
233 } else if (!strcmp(sformat, "H261")) {
234 iformat = MEDIA_FORMAT_H261;
236 } else if (!strcmp(sformat, "H263")) {
237 iformat = MEDIA_FORMAT_H263;
239 } else if (!strcmp(sformat, "H263P")) {
240 iformat = MEDIA_FORMAT_H263P;
242 } else if (!strcmp(sformat, "H264_SP")) {
243 iformat = MEDIA_FORMAT_H264_SP;
245 } else if (!strcmp(sformat, "H264_MP")) {
246 iformat = MEDIA_FORMAT_H264_MP;
248 } else if (!strcmp(sformat, "H264_HP")) {
249 iformat = MEDIA_FORMAT_H264_HP;
251 } else if (!strcmp(sformat, "MPEG4_SP")) {
252 iformat = MEDIA_FORMAT_MPEG4_SP;
254 } else if (!strcmp(sformat, "MPEG4_ASP")) {
255 iformat = MEDIA_FORMAT_MPEG4_ASP;
257 } else if (!strcmp(sformat, "AMR_NB")) {
258 iformat = MEDIA_FORMAT_AMR_NB;
260 } else if (!strcmp(sformat, "AMR_WB")) {
261 iformat = MEDIA_FORMAT_AMR_WB;
263 } else if (!strcmp(sformat, "AAC_LC")) {
264 iformat = MEDIA_FORMAT_AAC_LC;
266 } else if (!strcmp(sformat, "AAC_HE")) {
267 iformat = MEDIA_FORMAT_AAC_HE;
269 } else if (!strcmp(sformat, "AAC_HE_PS")) {
270 iformat = MEDIA_FORMAT_AAC_HE_PS;
272 } else if (!strcmp(sformat, "MP3")) {
273 iformat = MEDIA_FORMAT_MP3;
275 } else if (!strcmp(sformat, "VORBIS")) {
276 iformat = MEDIA_FORMAT_VORBIS;
278 } else if (!strcmp(sformat, "FLAC")) {
279 iformat = MEDIA_FORMAT_FLAC;
281 } else if (!strcmp(sformat, "WMAV1")) {
282 iformat = MEDIA_FORMAT_WMAV1;
284 } else if (!strcmp(sformat, "WMAV2")) {
285 iformat = MEDIA_FORMAT_WMAV2;
287 } else if (!strcmp(sformat, "WMAPRO")) {
288 iformat = MEDIA_FORMAT_WMAPRO;
293 LOGD("sformat : %x", iformat);
297 int mc_ini_load(mc_ini_t *ini)
299 gchar cname[CNAME_SIZE];
301 dictionary *dict = NULL;
304 memset(ini, 0, sizeof(mc_ini_t));
306 ini->codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
308 _mc_ini_check_ini_status();
310 /* first, try to load existing ini file */
311 dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
313 /* if no file exists. create one with set of default values */
315 #ifdef MEDIA_CODEC_DEFAULT_INI
316 LOGD("No inifile found. codec will create default inifile.\n");
317 if (FALSE == _generate_default_ini()) {
318 LOGW("Creating default inifile failed. Media Codec will use default values.\n");
320 /* load default ini */
321 dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
324 LOGD("No ini file found. \n");
325 return LOGERROR_FILE_NOT_FOUND;
330 if (dict) {/* if dict is available */
332 MEDIA_CODEC_INI_GET_STRING(dict, ini->port_name, "port_in_use:media_codec_port", DEFAULT_PORT);
334 for (i = 0; i < ini->codec_list; i++) {
335 memset(cname, 0x00, CNAME_SIZE);
336 snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
337 int len = strlen(cname);
338 ini->codec[i].codec_id = general_codec_list[i].ctype;
339 snprintf(cname+len, CNAME_SIZE - len, "%s", ":hw_decoder");
340 MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[0], cname, DEFAULT_VALUE);
341 snprintf(cname+len, CNAME_SIZE - len, "%s", ":hw_encoder");
342 MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[1], cname, DEFAULT_VALUE);
343 snprintf(cname+len, CNAME_SIZE - len, "%s", ":sw_decoder");
344 MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[2], cname, DEFAULT_VALUE);
345 snprintf(cname+len, CNAME_SIZE - len, "%s", ":sw_encoder");
346 MEDIA_CODEC_INI_GET_STRING_FROM_LIST(dict, ini->codec[i].codec_info[3], cname, DEFAULT_VALUE);
348 } else {/* if dict is not available just fill the structure with default value */
350 LOGW("failed to load ini. using hardcoded default\n");
352 snprintf(ini->port_name, sizeof(ini->port_name), "%s", DEFAULT_PORT);
353 for (i = 0; i < ini->codec_list; i++) {
354 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].name, DEFAULT_HW_DECODER_NAME);
355 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].mime, DEFAULT_HW_DECODER_MIME);
356 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[0].format, DEFAULT_HW_DECODER_FORMAT);
358 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].name, DEFAULT_HW_ENCODER_NAME);
359 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].mime, DEFAULT_HW_ENCODER_MIME);
360 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[1].format, DEFAULT_HW_ENCODER_FORMAT);
362 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].name, DEFAULT_SW_DECODER_NAME);
363 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].mime, DEFAULT_SW_DECODER_MIME);
364 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[2].format, DEFAULT_SW_DECODER_FORMAT);
366 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].name, DEFAULT_SW_ENCODER_NAME);
367 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].mime, DEFAULT_SW_ENCODER_MIME);
368 MEDIA_CODEC_GET_DEFAULT_LIST(ini->codec[i].codec_info[3].format, DEFAULT_SW_ENCODER_FORMAT);
372 if (0 == strcmp(ini->port_name, "GST_PORT"))
373 ini->port_type = GST_PORT;
375 LOGE("Invalid port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
376 iniparser_freedict(dict);
379 LOGD("The port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
381 for (i = 0; i < ini->codec_list; i++) {
382 memset(cname, 0x00, CNAME_SIZE);
383 snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
384 int len = strlen(cname);
386 snprintf(cname+len, CNAME_SIZE-len, "%s", ":hw_decoder");
387 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[0], cname);
388 snprintf(cname+len, CNAME_SIZE-len, "%s", ":hw_encoder");
389 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[1], cname);
390 snprintf(cname+len, CNAME_SIZE-len, "%s", ":sw_decoder");
391 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[2], cname);
392 snprintf(cname+len, CNAME_SIZE-len, "%s", ":sw_encoder");
393 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[3], cname);
396 /* free dict as we got our own structure */
397 iniparser_freedict(dict);
400 LOGD("codec settings -----------------------------------\n");
403 LOGD("port_name: %s\n", ini->port_name);
404 LOGD("port_type : %d\n", ini->port_type);
406 return MC_ERROR_NONE;
408 return MC_COURRPTED_INI;
412 static void _mc_ini_check_ini_status(void)
414 struct stat ini_buff;
416 if (g_stat(MEDIA_CODEC_INI_DEFAULT_PATH, &ini_buff) < 0) {
417 LOGW("failed to get codec ini status\n");
419 if (ini_buff.st_size < 5) {
420 LOGW("codec.ini file size=%d, Corrupted! So, Removed\n", (int)ini_buff.st_size);
422 if (g_remove(MEDIA_CODEC_INI_DEFAULT_PATH) == -1)
423 LOGE("failed to delete corrupted ini");
428 #ifdef MEDIA_CODEC_DEFAULT_INI
429 static gboolean _generate_default_ini(void)
432 gchar *default_ini = MEDIA_CODEC_DEFAULT_INI;
434 /* create new file */
435 fp = fopen(MEDIA_CODEC_INI_DEFAULT_PATH, "wt");
440 /* writing default ini file */
441 if (strlen(default_ini) !=
442 fwrite(default_ini, 1, strlen(default_ini), fp)) {
451 #endif /* #ifdef _MEDIA_CODEC_INI_C_ */