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
52 gchar cname[MEDIA_CODEC_INI_MAX_STRLEN];
53 mediacodec_codec_type_e ctype;
56 static codec_list_t general_codec_list[]={
57 {"h261", MEDIACODEC_H261},
58 {"h263", MEDIACODEC_H263},
59 {"h264", MEDIACODEC_H264},
60 {"mjpeg", MEDIACODEC_MJPEG},
61 {"mpeg1", MEDIACODEC_MPEG1},
62 {"mpeg2", MEDIACODEC_MPEG2},
63 {"mpeg4", MEDIACODEC_MPEG4},
64 {"hevc", MEDIACODEC_HEVC},
65 {"vp8", MEDIACODEC_VP8},
66 {"vp9", MEDIACODEC_VP9},
67 {"vc1", MEDIACODEC_VC1},
68 {"aac_lc", MEDIACODEC_AAC_LC},
69 {"aac_he", MEDIACODEC_AAC_HE},
70 {"aac_he_ps", MEDIACODEC_AAC_HE_PS},
71 {"mp3", MEDIACODEC_MP3},
72 {"amr_nb", MEDIACODEC_AMR_NB},
73 {"amr_wb", MEDIACODEC_AMR_WB},
74 {"vorbis", MEDIACODEC_VORBIS},
75 {"flac", MEDIACODEC_FLAC},
76 {"wmav1", MEDIACODEC_WMAV1},
77 {"wmav2", MEDIACODEC_WMAV2},
78 {"wmapro", MEDIACODEC_WMAPRO},
81 /* internal functions, macros here */
82 #ifdef MEDIA_CODEC_DEFAULT_INI
83 static gboolean _generate_default_ini(void);
86 static void _mc_ini_check_ini_status(void);
89 #define MEDIA_CODEC_INI_GET_STRING( x_dict, x_item, x_ini, x_default ) \
92 gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
95 ( strlen( str ) > 0 ) && \
96 ( strlen( str ) < MEDIA_CODEC_INI_MAX_STRLEN ) ) \
98 strncpy ( x_item, str, strlen(str)+1 ); \
102 strncpy ( x_item, x_default, strlen(x_default)+1 ); \
106 #define MEDIA_CODEC_INI_GET_STRING_FROM_LIST( x_dict, x_list, x_ini, x_default ) \
109 char *token = NULL; \
110 char *usr_ptr = NULL; \
112 const char *delimiters = " ,"; \
113 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
114 MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
115 token = strtok_r( temp_arr, delimiters, &usr_ptr ); \
119 strncpy(x_list.name,token, MEDIA_CODEC_INI_MAX_STRLEN-1 );\
120 else if (index == 1) \
121 strncpy(x_list.mime,token, MEDIA_CODEC_INI_MAX_STRLEN-1 );\
122 else if (index == 2) \
123 strncpy(x_list.format,token, MEDIA_CODEC_INI_MAX_STRLEN-1 );\
125 token = strtok_r( NULL, delimiters, &usr_ptr ); \
129 #define MEDIA_CODEC_INI_GET_COLOR( x_dict, x_item, x_ini, x_default ) \
132 gchar* str = iniparser_getstring(x_dict, x_ini, x_default); \
135 ( strlen( str ) > 0 ) && \
136 ( strlen( str ) < MEDIA_CODEC_INI_MAX_STRLEN ) ) \
138 x_item = (guint) strtoul(str, NULL, 16); \
142 x_item = (guint) strtoul(x_default, NULL, 16); \
146 /* x_ini is the list of index to set TRUE at x_list[index] */
147 #define MEDIA_CODEC_INI_GET_BOOLEAN_FROM_LIST( x_dict, x_list, x_list_max, x_ini, x_default ) \
151 const char *delimiters = " ,"; \
152 char *usr_ptr = NULL; \
153 char *token = NULL; \
154 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
155 MEDIA_CODEC_INI_GET_STRING( x_dict, temp_arr, x_ini, x_default); \
156 token = strtok_r( temp_arr, delimiters, &usr_ptr ); \
159 index = atoi(token); \
160 if (index < 0 || index > x_list_max -1) \
162 LOGW("%d is not valid index\n", index); \
166 x_list[index] = TRUE; \
168 token = strtok_r( NULL, delimiters, &usr_ptr ); \
172 /* x_ini is the list of value to be set at x_list[index] */
173 #define MEDIA_CODEC_INI_GET_INT_FROM_LIST( x_dict, x_list, x_list_max, x_ini, x_default ) \
178 const char *delimiters = " ,"; \
179 char *usr_ptr = NULL; \
180 char *token = NULL; \
181 gchar temp_arr[MEDIA_CODEC_INI_MAX_STRLEN] = {0}; \
182 MEDIA_CODEC_INI_GET_STRING(x_dict, temp_arr, x_ini, x_default); \
183 token = strtok_r( temp_arr, delimiters, &usr_ptr ); \
186 if ( index > x_list_max -1) \
188 LOGE("%d is not valid index\n", index); \
193 value = atoi(token); \
194 x_list[index] = value; \
197 token = strtok_r( NULL, delimiters, &usr_ptr ); \
201 #define MEDIA_CODEC_GET_DEFAULT_LIST( x_list, x_default ) \
204 strncpy( x_list, x_default, MEDIA_CODEC_INI_MAX_STRLEN - 1);\
206 #define MEDIA_CODEC_PRINT_LIST( x_list, x_message ) \
209 codec_info_t codec_list= x_list;\
210 printf("%s =", x_message);\
211 printf("%s %s %s\n", codec_list.name, codec_list.mime, codec_list.format);\
214 media_format_mimetype_e _mc_convert_media_format_str_to_int(char *sformat )
217 media_format_mimetype_e iformat = MEDIA_FORMAT_I420;
218 if (!strcmp(sformat,"I420")) {
219 iformat = MEDIA_FORMAT_I420;
221 } else if (!strcmp(sformat,"NV12")) {
222 iformat = MEDIA_FORMAT_NV12;
224 } else if (!strcmp(sformat,"NV12T")) {
225 iformat = MEDIA_FORMAT_NV12T;
227 } else if (!strcmp(sformat,"YV12")) {
228 iformat = MEDIA_FORMAT_YV12;
230 } else if (!strcmp(sformat,"NV21")) {
231 iformat = MEDIA_FORMAT_NV21;
233 } else if (!strcmp(sformat,"NV16")) {
234 iformat = MEDIA_FORMAT_NV16;
235 } else if (!strcmp(sformat,"YUYV")) {
236 iformat = MEDIA_FORMAT_YUYV;
238 } else if (!strcmp(sformat,"UYVY")) {
239 iformat = MEDIA_FORMAT_UYVY;
241 } else if (!strcmp(sformat,"422P")) {
242 iformat = MEDIA_FORMAT_422P;
244 } else if (!strcmp(sformat,"RGB565")) {
245 iformat = MEDIA_FORMAT_RGB565;
247 } else if (!strcmp(sformat,"RGB888")) {
248 iformat = MEDIA_FORMAT_RGB888;
250 } else if (!strcmp(sformat,"RGBA")) {
251 iformat = MEDIA_FORMAT_RGBA;
253 } else if (!strcmp(sformat,"ARGB")) {
254 iformat = MEDIA_FORMAT_ARGB;
256 } else if (!strcmp(sformat,"PCM")) {
257 iformat = MEDIA_FORMAT_PCM;
259 } else if (!strcmp(sformat,"H261")) {
260 iformat = MEDIA_FORMAT_H261;
262 } else if (!strcmp(sformat,"H263")) {
263 iformat = MEDIA_FORMAT_H263;
265 } else if (!strcmp(sformat,"H263P")) {
266 iformat = MEDIA_FORMAT_H263P;
268 } else if (!strcmp(sformat,"H264_SP")) {
269 iformat = MEDIA_FORMAT_H264_SP;
271 } else if (!strcmp(sformat,"H264_MP")) {
272 iformat = MEDIA_FORMAT_H264_MP;
274 } else if (!strcmp(sformat,"H264_HP")) {
275 iformat = MEDIA_FORMAT_H264_HP;
277 } else if (!strcmp(sformat,"MPEG4_SP")) {
278 iformat = MEDIA_FORMAT_MPEG4_SP;
280 } else if (!strcmp(sformat,"MPEG4_ASP")) {
281 iformat = MEDIA_FORMAT_MPEG4_ASP;
283 } else if (!strcmp(sformat,"AMR_NB")) {
284 iformat = MEDIA_FORMAT_AMR_NB;
286 } else if (!strcmp(sformat,"AMR_WB")) {
287 iformat = MEDIA_FORMAT_AMR_WB;
289 } else if (!strcmp(sformat,"AAC_LC")) {
290 iformat = MEDIA_FORMAT_AAC_LC;
292 } else if (!strcmp(sformat,"AAC_HE")) {
293 iformat = MEDIA_FORMAT_AAC_HE;
295 } else if (!strcmp(sformat,"AAC_HE_PS")) {
296 iformat = MEDIA_FORMAT_AAC_HE_PS;
298 } else if (!strcmp(sformat,"MP3")) {
299 iformat = MEDIA_FORMAT_MP3;
301 } else if (!strcmp(sformat,"VORBIS")) {
302 iformat = MEDIA_FORMAT_VORBIS;
304 } else if (!strcmp(sformat,"FLAC")) {
305 iformat = MEDIA_FORMAT_FLAC;
307 } else if (!strcmp(sformat,"WMAV1")) {
308 iformat = MEDIA_FORMAT_WMAV1;
310 } else if (!strcmp(sformat,"WMAV2")) {
311 iformat = MEDIA_FORMAT_WMAV2;
313 } else if (!strcmp(sformat,"WMAPRO")) {
314 iformat = MEDIA_FORMAT_WMAPRO;
319 LOGD("sformat : %x", iformat);
323 int mc_ini_load(mc_ini_t *ini)
325 gchar cname[CNAME_SIZE];
327 dictionary *dict = NULL;
329 static const int codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
331 _mc_ini_check_ini_status();
333 /* first, try to load existing ini file */
334 dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
336 /* if no file exists. create one with set of default values */
338 #ifdef MEDIA_CODEC_DEFAULT_INI
339 LOGD("No inifile found. codec will create default inifile.\n");
340 if (FALSE == _generate_default_ini()) {
341 LOGW("Creating default inifile failed. Media Codec will use default values.\n");
343 /* load default ini */
344 dict = iniparser_load(MEDIA_CODEC_INI_DEFAULT_PATH);
347 LOGD("No ini file found. \n");
348 return LOGERROR_FILE_NOT_FOUND;
353 memset(ini, 0, sizeof(mc_ini_t));
355 if (dict) {/* if dict is available */
357 MEDIA_CODEC_INI_GET_STRING( dict, ini->port_name, "port_in_use:media_codec_port",DEFAULT_PORT);
359 for (i = 0; i < codec_list; i++) {
360 memset(cname, 0x00, CNAME_SIZE);
361 snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
362 int len = strlen(cname);
364 ini->codec[i].codec_id = general_codec_list[i].ctype;
365 snprintf(cname+len, CNAME_SIZE-len, "%s", ":hw_decoder");
366 MEDIA_CODEC_INI_GET_STRING_FROM_LIST( dict, ini->codec[i].codec_info[0], cname, DEFAULT_VALUE);
367 snprintf(cname+len, CNAME_SIZE-len, "%s",":hw_encoder");
368 MEDIA_CODEC_INI_GET_STRING_FROM_LIST( dict, ini->codec[i].codec_info[1], cname, DEFAULT_VALUE);
369 snprintf(cname+len, CNAME_SIZE-len, "%s",":sw_decoder");
370 MEDIA_CODEC_INI_GET_STRING_FROM_LIST( dict, ini->codec[i].codec_info[2], cname, DEFAULT_VALUE);
371 snprintf(cname+len, CNAME_SIZE-len, "%s",":sw_encoder");
372 MEDIA_CODEC_INI_GET_STRING_FROM_LIST( dict, ini->codec[i].codec_info[3], cname, DEFAULT_VALUE);
374 } else {/* if dict is not available just fill the structure with default value */
376 LOGW("failed to load ini. using hardcoded default\n");
378 snprintf(ini->port_name, sizeof(ini->port_name), "%s", DEFAULT_PORT);
379 for (i = 0;i < codec_list; i++) {
380 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[0].name, DEFAULT_HW_DECODER_NAME);
381 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[0].mime, DEFAULT_HW_DECODER_MIME);
382 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[0].format, DEFAULT_HW_DECODER_FORMAT);
384 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[1].name, DEFAULT_HW_ENCODER_NAME);
385 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[1].mime, DEFAULT_HW_ENCODER_MIME);
386 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[1].format, DEFAULT_HW_ENCODER_FORMAT);
388 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[2].name, DEFAULT_SW_DECODER_NAME);
389 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[2].mime, DEFAULT_SW_DECODER_MIME);
390 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[2].format, DEFAULT_SW_DECODER_FORMAT);
392 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[3].name, DEFAULT_SW_ENCODER_NAME);
393 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[3].mime, DEFAULT_SW_ENCODER_MIME);
394 MEDIA_CODEC_GET_DEFAULT_LIST( ini->codec[i].codec_info[3].format, DEFAULT_SW_ENCODER_FORMAT);
398 if (0 == strcmp(ini->port_name, "GST_PORT"))
399 ini->port_type = GST_PORT;
401 LOGE("Invalid port is set to [%s] [%d]\n", ini->port_name,ini->port_type);
402 iniparser_freedict(dict);
405 LOGD("The port is set to [%s] [%d]\n", ini->port_name, ini->port_type);
407 for (i = 0;i < codec_list; i++) {
408 memset(cname, 0x00, CNAME_SIZE);
409 snprintf(cname, CNAME_SIZE, "%s", general_codec_list[i].cname);
410 int len = strlen(cname);
412 snprintf(cname+len, CNAME_SIZE-len, "%s",":hw_decoder");
413 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[0],cname);
414 snprintf(cname+len, CNAME_SIZE-len, "%s",":hw_encoder");
415 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[1],cname);
416 snprintf(cname+len, CNAME_SIZE-len, "%s",":sw_decoder");
417 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[2],cname);
418 snprintf(cname+len, CNAME_SIZE-len, "%s",":sw_encoder");
419 MEDIA_CODEC_PRINT_LIST(ini->codec[i].codec_info[3],cname);
422 /* free dict as we got our own structure */
423 iniparser_freedict(dict);
426 LOGD("codec settings -----------------------------------\n");
429 LOGD("port_name: %s\n", ini->port_name);
430 LOGD("port_type : %d\n", ini->port_type);
432 return MC_ERROR_NONE;
434 return MC_COURRPTED_INI;
438 static void _mc_ini_check_ini_status(void)
440 struct stat ini_buff;
442 if (g_stat(MEDIA_CODEC_INI_DEFAULT_PATH, &ini_buff) < 0) {
443 LOGW("failed to get codec ini status\n");
445 if (ini_buff.st_size < 5) {
446 LOGW("codec.ini file size=%d, Corrupted! So, Removed\n", (int)ini_buff.st_size);
448 if (g_remove(MEDIA_CODEC_INI_DEFAULT_PATH) == -1) {
449 LOGE("failed to delete corrupted ini");
455 #ifdef MEDIA_CODEC_DEFAULT_INI
456 static gboolean _generate_default_ini(void)
459 gchar *default_ini = MEDIA_CODEC_DEFAULT_INI;
461 /* create new file */
462 fp = fopen(MEDIA_CODEC_INI_DEFAULT_PATH, "wt");
468 /* writing default ini file */
469 if (strlen(default_ini) !=
470 fwrite(default_ini, 1, strlen(default_ini), fp)) {
480 void _mc_create_decoder_map_from_ini(mc_handle_t *mediacodec)
482 int indx = 0, count = 0;
483 int codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
484 for (indx=0;indx <codec_list;indx++) {
485 if (strcmp(mediacodec->ini.codec[indx].codec_info[0].name, "")) {
486 mediacodec->decoder_map[count].id = mediacodec->ini.codec[indx].codec_id;
487 mediacodec->decoder_map[count].hardware = 1; // hardware
488 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini.codec[indx].codec_info[0].name;
489 mediacodec->decoder_map[count].type.mime = mediacodec->ini.codec[indx].codec_info[0].mime;
490 mediacodec->decoder_map[count].type.out_format= _mc_convert_media_format_str_to_int(mediacodec->ini.codec[indx].codec_info[0].format);
494 if (strcmp(mediacodec->ini.codec[indx].codec_info[2].name, "")) {
495 mediacodec->decoder_map[count].id = mediacodec->ini.codec[indx].codec_id;
496 mediacodec->decoder_map[count].hardware = 0; // software
497 mediacodec->decoder_map[count].type.factory_name = mediacodec->ini.codec[indx].codec_info[2].name;
498 mediacodec->decoder_map[count].type.mime = mediacodec->ini.codec[indx].codec_info[2].mime;
499 mediacodec->decoder_map[count].type.out_format= _mc_convert_media_format_str_to_int(mediacodec->ini.codec[indx].codec_info[2].format);
503 mediacodec->num_supported_decoder = count;
508 void _mc_create_encoder_map_from_ini(mc_handle_t *mediacodec)
510 int indx = 0, count = 0;
511 int codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
513 for (indx=0;indx <codec_list;indx++) {
514 if (strcmp(mediacodec->ini.codec[indx].codec_info[1].name, "")) {
515 mediacodec->encoder_map[count].id = mediacodec->ini.codec[indx].codec_id;
516 mediacodec->encoder_map[count].hardware = 1;
517 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini.codec[indx].codec_info[1].name;
518 mediacodec->encoder_map[count].type.mime = mediacodec->ini.codec[indx].codec_info[1].mime;
519 mediacodec->encoder_map[count].type.out_format= _mc_convert_media_format_str_to_int(mediacodec->ini.codec[indx].codec_info[1].format);
523 if (strcmp(mediacodec->ini.codec[indx].codec_info[3].name, "")) {
524 mediacodec->encoder_map[count].id = mediacodec->ini.codec[indx].codec_id;
525 mediacodec->encoder_map[count].hardware = 0;
526 mediacodec->encoder_map[count].type.factory_name = mediacodec->ini.codec[indx].codec_info[3].name;
527 mediacodec->encoder_map[count].type.mime = mediacodec->ini.codec[indx].codec_info[3].mime;
528 mediacodec->encoder_map[count].type.out_format= _mc_convert_media_format_str_to_int(mediacodec->ini.codec[indx].codec_info[3].format);
532 mediacodec->num_supported_encoder = count;
536 void _mc_create_codec_map_from_ini(mc_handle_t *mediacodec, mc_codec_spec_t *spec_emul)
538 int indx = 0, count = 0;
539 int codec_list = sizeof(general_codec_list) / sizeof(general_codec_list[0]);
540 for (indx=0;indx <codec_list;indx++) {
541 if (strcmp(mediacodec->ini.codec[indx].codec_info[0].name, "")) {
542 spec_emul[count].codec_id = mediacodec->ini.codec[indx].codec_id;
543 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_HW;
544 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
547 if (strcmp(mediacodec->ini.codec[indx].codec_info[1].name, "")) {
548 spec_emul[count].codec_id = mediacodec->ini.codec[indx].codec_id;
549 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_HW;
550 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
553 if (strcmp(mediacodec->ini.codec[indx].codec_info[2].name, "")) {
554 spec_emul[count].codec_id = mediacodec->ini.codec[indx].codec_id;
555 spec_emul[count].codec_type = MEDIACODEC_DECODER | MEDIACODEC_SUPPORT_TYPE_SW;
556 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
559 if (strcmp(mediacodec->ini.codec[indx].codec_info[3].name, "")) {
560 spec_emul[count].codec_id = mediacodec->ini.codec[indx].codec_id;
561 spec_emul[count].codec_type = MEDIACODEC_ENCODER | MEDIACODEC_SUPPORT_TYPE_SW;
562 spec_emul[count].port_type = MEDIACODEC_PORT_TYPE_GST;
567 mediacodec->num_supported_codecs = count;
571 #endif /* #ifdef _MEDIA_CODEC_INI_C_ */