[mpp_cfg]: Add general config function

1. Add struct config function.
2. Do not check data type when data size is matched.
3. Use type/size/offset to define data and update info.

Change-Id: Ief21107cf9a8a6b89c2b01c1a430d88e0cf827f4
Signed-off-by: Herman Chen <herman.chen@rock-chips.com>
This commit is contained in:
Herman Chen 2021-01-05 16:04:39 +08:00
parent 2e5ea77cb2
commit e0df28b509
6 changed files with 236 additions and 131 deletions

View File

@ -34,12 +34,14 @@ MPP_RET mpp_enc_cfg_set_u32(MppEncCfg cfg, const char *name, RK_U32 val);
MPP_RET mpp_enc_cfg_set_s64(MppEncCfg cfg, const char *name, RK_S64 val);
MPP_RET mpp_enc_cfg_set_u64(MppEncCfg cfg, const char *name, RK_U64 val);
MPP_RET mpp_enc_cfg_set_ptr(MppEncCfg cfg, const char *name, void *val);
MPP_RET mpp_enc_cfg_set_st(MppEncCfg cfg, const char *name, void *val);
MPP_RET mpp_enc_cfg_get_s32(MppEncCfg cfg, const char *name, RK_S32 *val);
MPP_RET mpp_enc_cfg_get_u32(MppEncCfg cfg, const char *name, RK_U32 *val);
MPP_RET mpp_enc_cfg_get_s64(MppEncCfg cfg, const char *name, RK_S64 *val);
MPP_RET mpp_enc_cfg_get_u64(MppEncCfg cfg, const char *name, RK_U64 *val);
MPP_RET mpp_enc_cfg_get_ptr(MppEncCfg cfg, const char *name, void **val);
MPP_RET mpp_enc_cfg_get_st(MppEncCfg cfg, const char *name, void *val);
void mpp_enc_cfg_show(void);

View File

@ -21,6 +21,7 @@ add_library(mpp_base STATIC
mpp_bitwrite.c
mpp_bitread.c
mpp_bitput.c
mpp_cfg.cpp
mpp_2str.c
)

82
mpp/base/mpp_cfg.cpp Normal file
View File

@ -0,0 +1,82 @@
/*
* Copyright 2020 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define MODULE_TAG "mpp_cfg"
#include "mpp_log.h"
#include "mpp_cfg.h"
const char *cfg_type_names[] = {
"RK_S32",
"RK_U32",
"RK_S64",
"RK_U64",
"void *",
"struct",
};
static void show_api_type_err(MppCfgApi *api, CfgType type)
{
mpp_err("cfg %s expect %s input NOT %s\n", api->name,
cfg_type_names[api->info.type],
cfg_type_names[type]);
}
MPP_RET check_cfg_api_info(MppCfgApi *api, CfgType type)
{
CfgType cfg_type = api->info.type;
RK_S32 cfg_size = api->info.size;
MPP_RET ret = MPP_OK;
switch (type) {
case CFG_FUNC_TYPE_St : {
if (cfg_type != type) {
show_api_type_err(api, type);
ret = MPP_NOK;
}
if (cfg_size <= 0) {
mpp_err("cfg %s found invalid size %d\n", api->name, cfg_size);
ret = MPP_NOK;
}
} break;
case CFG_FUNC_TYPE_Ptr : {
if (cfg_type != type) {
show_api_type_err(api, type);
ret = MPP_NOK;
}
} break;
case CFG_FUNC_TYPE_S32 :
case CFG_FUNC_TYPE_U32 : {
if (cfg_size != sizeof(RK_S32)) {
show_api_type_err(api, type);
ret = MPP_NOK;
}
} break;
case CFG_FUNC_TYPE_S64 :
case CFG_FUNC_TYPE_U64 : {
if (cfg_size != sizeof(RK_S64)) {
show_api_type_err(api, type);
ret = MPP_NOK;
}
} break;
default : {
mpp_err("cfg %s found invalid cfg type %d\n", api->name, type);
ret = MPP_NOK;
} break;
}
return ret;
}

View File

@ -16,6 +16,8 @@
#define MODULE_TAG "mpp_enc_cfg"
#include <string.h>
#include "rk_venc_cfg.h"
#include "mpp_env.h"
@ -25,6 +27,7 @@
#include "mpp_common.h"
#include "mpp_thread.h"
#include "mpp_cfg.h"
#include "mpp_enc_cfg_impl.h"
#define MPP_ENC_CFG_DBG_FUNC (0x00000001)
@ -41,111 +44,71 @@
RK_U32 mpp_enc_cfg_debug = 0;
#define EXPAND_AS_ENUM(name, func, type) \
SET_##name, \
GET_##name,
/* Expand to enum
typedef enum CfgType_e {
SET_S32,
GET_S32,
SET_U32,
GET_U32,
SET_S64,
GET_S64,
SET_U64,
GET_U64,
SET_PTR,
GET_PTR,
CFG_FUNC_TYPE_BUTT,
} CfgType;
*/
#define EXPAND_AS_TYPE(name, func, type) \
typedef MPP_RET (*CfgSet##func)(void *ctx, type val); \
typedef MPP_RET (*CfgGet##func)(void *ctx, type *val);
/* Expand to function type
typedef MPP_RET (*CfgSetS32)(void *ctx, RK_S32 val);
typedef MPP_RET (*CfgGetS32)(void *ctx, RK_S32 *val);
typedef MPP_RET (*CfgSetU32)(void *ctx, RK_U32 val);
typedef MPP_RET (*CfgGetU32)(void *ctx, RK_U32 *val);
typedef MPP_RET (*CfgSetS64)(void *ctx, RK_S64 val);
typedef MPP_RET (*CfgGetS64)(void *ctx, RK_S64 *val);
typedef MPP_RET (*CfgSetU64)(void *ctx, RK_U64 val);
typedef MPP_RET (*CfgGetU64)(void *ctx, RK_U64 *val);
typedef MPP_RET (*CfgSetPtr)(void *ctx, void *val);
typedef MPP_RET (*CfgGetPtr)(void *ctx, void **val);
*/
#define EXPAND_AS_NAME(name, func, type) \
#type, \
#type"*",
/* Expand to name
static const char *cfg_func_names[] = {
"RK_S32",
"RK_S32*",
"RK_U32",
"RK_U32*",
"RK_S64",
"RK_S64*",
"RK_U64",
"RK_U64*",
"void *",
"void **",
};
*/
#define TYPE_ENTRY_TABLE(ENTRY) \
ENTRY(S32, S32, RK_S32) \
ENTRY(U32, U32, RK_U32) \
ENTRY(S64, S64, RK_S64) \
ENTRY(U64, U64, RK_U64) \
ENTRY(PTR, Ptr, void *)
typedef enum CfgType_e {
TYPE_ENTRY_TABLE(EXPAND_AS_ENUM)
CFG_FUNC_TYPE_BUTT,
} CfgType;
TYPE_ENTRY_TABLE(EXPAND_AS_TYPE)
static const char *cfg_func_names[] = {
TYPE_ENTRY_TABLE(EXPAND_AS_NAME)
};
#define EXPAND_AS_FUNC(base, name, func_type, in_type, flag, field0, field1) \
MPP_RET set_##base##_##name(MppEncCfgSet *cfg, in_type name) \
#define EXPAND_AS_FUNC(base, name, cfg_type, in_type, flag, field_change, field_data) \
MPP_RET set_##base##_##name(void *cfg, MppCfgApi *api, in_type name) \
{ \
char *base = (char *)cfg; \
CfgDataInfo *info = &api->info; \
CfgDataInfo *update = &api->update; \
\
mpp_enc_cfg_dbg_func("enter\n"); \
if (cfg->field0.field1 != (in_type)name || SET_##func_type == SET_PTR) { \
cfg->field0.field1 = (in_type)name; \
cfg->field0.change |= flag; \
switch (CFG_FUNC_TYPE_##cfg_type) { \
case CFG_FUNC_TYPE_S32 : \
case CFG_FUNC_TYPE_U32 : \
case CFG_FUNC_TYPE_S64 : \
case CFG_FUNC_TYPE_U64 : { \
if (memcmp(base + info->offset, &name, info->size)) { \
memcpy(base + info->offset, &name, info->size); \
*((RK_U32 *)(base + update->offset)) |= flag; \
} \
} break; \
case CFG_FUNC_TYPE_Ptr : { \
memcpy(base + info->offset, &name, info->size); \
*((RK_U32 *)(base + update->offset)) |= flag; \
} break; \
case CFG_FUNC_TYPE_St : { \
if (memcmp(base + info->offset, (void *)((long)name), info->size)) { \
memcpy(base + info->offset, (void *)((long)name), info->size); \
*((RK_U32 *)(base + update->offset)) |= flag; \
} \
} break; \
default : { \
} break; \
} \
return MPP_OK; \
} \
MPP_RET get_##base##_##name(MppEncCfgSet *cfg, in_type *name) \
MPP_RET get_##base##_##name(void *cfg, MppCfgApi *api, in_type *name) \
{ \
char *base = (char *)cfg; \
CfgDataInfo *info = &api->info; \
\
mpp_enc_cfg_dbg_func("enter\n"); \
*name = (in_type)cfg->field0.field1; \
memcpy(name, base + info->offset, info->size); \
return MPP_OK; \
}
#define EXPAND_AS_API(base, name, func_type, in_type, flag, field0, field1) \
static MppEncCfgApi api_##base##_##name = \
#define EXPAND_AS_API(base, name, cfg_type, in_type, flag, field_change, field_data) \
static MppCfgApi api_##base##_##name = \
{ \
#base":"#name, \
SET_##func_type, \
GET_##func_type, \
{ \
CFG_FUNC_TYPE_##cfg_type, \
sizeof((((MppEncCfgSet *)0)->field_change.field_data)), \
(RK_U32)((long)&(((MppEncCfgSet *)0)->field_change.field_data)), \
}, \
{ \
CFG_FUNC_TYPE_U32, \
sizeof((((MppEncCfgSet *)0)->field_change.change)), \
(RK_U32)((long)&(((MppEncCfgSet *)0)->field_change.change)), \
}, \
(void *)set_##base##_##name, \
(void *)get_##base##_##name, \
};
#define EXPAND_AS_ARRAY(base, name, func_type, in_type, flag, field0, field1) \
#define EXPAND_AS_ARRAY(base, name, cfg_type, in_type, flag, field_change, field_data) \
&api_##base##_##name,
#define EXPAND_AS_STRLEN(base, name, func_type, in_type, flag, field0, field1) \
#define EXPAND_AS_STRLEN(base, name, cfg_type, in_type, flag, field_change, field_data) \
const_strlen( #base":"#name ) +
#define ENTRY_TABLE(ENTRY) \
@ -243,9 +206,9 @@ static const char *cfg_func_names[] = {
ENTRY(vp8, disable_ivf, S32, RK_S32, MPP_ENC_VP8_CFG_CHANGE_DIS_IVF, codec.vp8, disable_ivf) \
/* jpeg config */ \
ENTRY(jpeg, quant, S32, RK_S32, MPP_ENC_JPEG_CFG_CHANGE_QP, codec.jpeg, quant) \
ENTRY(jpeg, qtable_y, PTR, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_y) \
ENTRY(jpeg, qtable_u, PTR, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_u) \
ENTRY(jpeg, qtable_v, PTR, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_v) \
ENTRY(jpeg, qtable_y, Ptr, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_y) \
ENTRY(jpeg, qtable_u, Ptr, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_u) \
ENTRY(jpeg, qtable_v, Ptr, RK_U8*, MPP_ENC_JPEG_CFG_CHANGE_QTABLE, codec.jpeg, qtable_v) \
ENTRY(jpeg, q_factor, S32, RK_S32, MPP_ENC_JPEG_CFG_CHANGE_QFACTOR, codec.jpeg, q_factor) \
ENTRY(jpeg, qf_max, S32, RK_S32, MPP_ENC_JPEG_CFG_CHANGE_QFACTOR, codec.jpeg, qf_max) \
ENTRY(jpeg, qf_min, S32, RK_S32, MPP_ENC_JPEG_CFG_CHANGE_QFACTOR, codec.jpeg, qf_min) \
@ -256,7 +219,7 @@ static const char *cfg_func_names[] = {
ENTRY_TABLE(EXPAND_AS_FUNC)
ENTRY_TABLE(EXPAND_AS_API)
static MppEncCfgApi *cfg_apis[] = {
static MppCfgApi *cfg_apis[] = {
ENTRY_TABLE(EXPAND_AS_ARRAY)
};
@ -367,7 +330,7 @@ MPP_RET mpp_enc_cfg_deinit(MppEncCfg cfg)
return MPP_OK;
}
#define ENC_CFG_SET_ACCESS(func_name, in_type, func_enum, func_type) \
#define ENC_CFG_SET_ACCESS(func_name, in_type, cfg_type) \
MPP_RET func_name(MppEncCfg cfg, const char *name, in_type val) \
{ \
if (NULL == cfg || NULL == name) { \
@ -380,24 +343,23 @@ MPP_RET mpp_enc_cfg_deinit(MppEncCfg cfg)
mpp_err_f("failed to set %s to %d\n", name, val); \
return MPP_NOK; \
} \
MppEncCfgApi *api = (MppEncCfgApi *)info; \
if (api->type_set != func_enum) { \
mpp_err_f("%s expect %s input NOT %s\n", api->name, \
cfg_func_names[api->type_set], \
cfg_func_names[func_enum]); \
MppCfgApi *api = (MppCfgApi *)info; \
if (check_cfg_api_info(api, CFG_FUNC_TYPE_##cfg_type)) { \
return MPP_NOK; \
} \
mpp_enc_cfg_dbg_set("name %s type %s\n", api->name, cfg_func_names[api->type_set]); \
MPP_RET ret = ((func_type)api->api_set)(&p->cfg, val); \
mpp_enc_cfg_dbg_set("name %s type %s\n", api->name, cfg_type_names[api->info.type]); \
MPP_RET ret = ((CfgSet##cfg_type)api->api_set)(&p->cfg, api, val); \
return ret; \
}
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_s32, RK_S32, SET_S32, CfgSetS32);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_u32, RK_U32, SET_U32, CfgSetU32);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_s64, RK_S64, SET_S64, CfgSetS64);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_u64, RK_U64, SET_U64, CfgSetU64);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_ptr, void *, SET_PTR, CfgSetPtr);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_s32, RK_S32, S32);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_u32, RK_U32, U32);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_s64, RK_S64, S64);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_u64, RK_U64, U64);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_ptr, void *, Ptr);
ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_st, void *, St);
#define ENC_CFG_GET_ACCESS(func_name, in_type, func_enum, func_type) \
#define ENC_CFG_GET_ACCESS(func_name, in_type, cfg_type) \
MPP_RET func_name(MppEncCfg cfg, const char *name, in_type *val) \
{ \
if (NULL == cfg || NULL == name) { \
@ -410,22 +372,21 @@ ENC_CFG_SET_ACCESS(mpp_enc_cfg_set_ptr, void *, SET_PTR, CfgSetPtr);
mpp_err_f("failed to set %s to %d\n", name, val); \
return MPP_NOK; \
} \
MppEncCfgApi *api = (MppEncCfgApi *)info; \
if (api->type_get != func_enum) { \
mpp_err_f("%s expect %s input not %s\n", api->name, \
cfg_func_names[api->type_get], \
cfg_func_names[func_enum]); \
MppCfgApi *api = (MppCfgApi *)info; \
if (check_cfg_api_info(api, CFG_FUNC_TYPE_##cfg_type)) { \
return MPP_NOK; \
} \
mpp_enc_cfg_dbg_get("name %s type %s\n", api->name, cfg_func_names[api->type_get]); \
MPP_RET ret = ((func_type)api->api_get)(&p->cfg, val); \
mpp_enc_cfg_dbg_get("name %s type %s\n", api->name, cfg_type_names[api->info.type]); \
MPP_RET ret = ((CfgGet##cfg_type)api->api_get)(&p->cfg, api, val); \
return ret; \
}
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_s32, RK_S32, GET_S32, CfgGetS32);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_u32, RK_U32, GET_U32, CfgGetU32);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_s64, RK_S64, GET_S64, CfgGetS64);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_u64, RK_U64, GET_U64, CfgGetU64);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_ptr, void *, GET_PTR, CfgGetPtr);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_s32, RK_S32, S32);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_u32, RK_U32, U32);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_s64, RK_S64, S64);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_u64, RK_U64, U64);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_ptr, void *, Ptr);
ENC_CFG_GET_ACCESS(mpp_enc_cfg_get_st, void , St);
void mpp_enc_cfg_show(void)
{
@ -434,9 +395,8 @@ void mpp_enc_cfg_show(void)
mpp_log("dumping valid configure string start\n");
for (i = 0; i < MPP_ARRAY_ELEMS(cfg_apis); i++)
mpp_log("%-25s type set:%s get:%s\n", cfg_apis[i]->name,
cfg_func_names[cfg_apis[i]->type_set],
cfg_func_names[cfg_apis[i]->type_get]);
mpp_log("%-25s type %s\n", cfg_apis[i]->name,
cfg_type_names[cfg_apis[i]->info.type]);
mpp_log("dumping valid configure string done\n");

71
mpp/inc/mpp_cfg.h Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright 2020 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MPP_CFG_H__
#define __MPP_CFG_H__
#include "rk_type.h"
#include "mpp_err.h"
typedef enum CfgType_e {
CFG_FUNC_TYPE_S32,
CFG_FUNC_TYPE_U32,
CFG_FUNC_TYPE_S64,
CFG_FUNC_TYPE_U64,
CFG_FUNC_TYPE_Ptr,
CFG_FUNC_TYPE_St,
CFG_FUNC_TYPE_BUTT,
} CfgType;
typedef struct CfgDataInfo_s {
CfgType type : 4;
RK_U32 size : 12;
RK_U32 offset : 16;
} CfgDataInfo;
typedef struct MppCfgApi_t {
const char *name;
CfgDataInfo info;
CfgDataInfo update;
void *api_set;
void *api_get;
} MppCfgApi;
typedef MPP_RET (*CfgSetS32)(void *cfg, MppCfgApi *api, RK_S32 val);
typedef MPP_RET (*CfgGetS32)(void *cfg, MppCfgApi *api, RK_S32 *val);
typedef MPP_RET (*CfgSetU32)(void *cfg, MppCfgApi *api, RK_U32 val);
typedef MPP_RET (*CfgGetU32)(void *cfg, MppCfgApi *api, RK_U32 *val);
typedef MPP_RET (*CfgSetS64)(void *cfg, MppCfgApi *api, RK_S64 val);
typedef MPP_RET (*CfgGetS64)(void *cfg, MppCfgApi *api, RK_S64 *val);
typedef MPP_RET (*CfgSetU64)(void *cfg, MppCfgApi *api, RK_U64 val);
typedef MPP_RET (*CfgGetU64)(void *cfg, MppCfgApi *api, RK_U64 *val);
typedef MPP_RET (*CfgSetPtr)(void *cfg, MppCfgApi *api, void *val);
typedef MPP_RET (*CfgGetPtr)(void *cfg, MppCfgApi *api, void **val);
typedef MPP_RET (*CfgSetSt) (void *cfg, MppCfgApi *api, void *val);
typedef MPP_RET (*CfgGetSt) (void *cfg, MppCfgApi *api, void *val);
#ifdef __cplusplus
extern "C" {
#endif
extern const char *cfg_type_names[];
MPP_RET check_cfg_api_info(MppCfgApi *api, CfgType type);
#ifdef __cplusplus
}
#endif
#endif /*__MPP_CFG_H__*/

View File

@ -45,15 +45,4 @@ typedef struct MppEncCfgSet_t {
MppEncOSDPlt plt_data;
} MppEncCfgSet;
/*
* MppEncCfgApi is the function set for configure MppEncCfgSet by name
*/
typedef struct MppEncCfgApi_t {
const char *name;
RK_S32 type_set;
RK_S32 type_get;
void *api_set;
void *api_get;
} MppEncCfgApi;
#endif /*__MPP_ENC_CFG_H__*/