feat[mpp_cfg]: Add ref_cfg json apply/extract api

1. Add mpp_enc_ref_cfg_apply/extract to mpp_enc_ref.
2. Add mpp_cfg_get_val to cfg_io.
3. Apply handles VLA resize from parsed cnt values.

Change-Id: Ibc7e50f3e58c5a008cad7270edc9ac0ea24afaac
Signed-off-by: Yanjun Liao <yanjun.liao@rock-chips.com>
This commit is contained in:
Yanjun Liao
2026-05-15 17:45:48 +08:00
committed by Herman Chen
parent 18ce1393c6
commit f80f253ccc
4 changed files with 150 additions and 3 deletions
+8 -3
View File
@@ -17,8 +17,7 @@
#ifndef RK_VENC_REF_H
#define RK_VENC_REF_H
#include "rk_type.h"
#include "mpp_err.h"
#include "rk_mpp_cfg.h"
/*
* MPP reference management system follows the model of H.264/H.265 reference
@@ -64,8 +63,10 @@
/* max 4 temporal layer */
#define MPP_ENC_MAX_TEMPORAL_LAYER_NUM 4
/* max 4 long-term reference frame */
/* max 16 long-term reference frame */
#define MPP_ENC_MAX_LT_REF_NUM 16
/* max 16 ref cfg entries (DPB size limit from H.264/H.265) */
#define MPP_ENC_MAX_REF_CFG_NUM 16
/*
* Group Of Picture (GOP) config is separated into three parts:
@@ -258,6 +259,10 @@ MPP_RET mpp_enc_ref_cfg_set_keep_cpb(MppEncRefCfg ref, RK_S32 keep);
MPP_RET mpp_enc_ref_cfg_get_preset(MppEncRefPreset *preset);
void mpp_enc_ref_cfg_show(void);
/* JSON config apply / extract */
MPP_RET mpp_enc_ref_cfg_apply(MppEncRefCfg ref, MppCfgStrFmt fmt, char *buf);
MPP_RET mpp_enc_ref_cfg_extract(MppEncRefCfg ref, MppCfgStrFmt fmt, char **buf);
#ifdef __cplusplus
}
#endif
+2
View File
@@ -81,6 +81,8 @@ rk_s32 mpp_cfg_add_detail(MppCfgObj root, MppCfgObj detail);
/* find by name string */
rk_s32 mpp_cfg_find(MppCfgObj *obj, MppCfgObj root, char *name, rk_s32 type);
/* get object value from the found cfg node */
rk_s32 mpp_cfg_get_val(MppCfgObj obj, MppCfgType type, MppCfgVal *val);
void mpp_cfg_dump(MppCfgObj obj, const char *func);
#define mpp_cfg_dump_f(obj) mpp_cfg_dump(obj, __FUNCTION__)
+20
View File
@@ -4018,6 +4018,26 @@ static void write_struct(MppCfgIoImpl *obj, MppTrie trie, MppCfgStrBuf *str,
}
}
rk_s32 mpp_cfg_get_val(MppCfgObj obj, MppCfgType type, MppCfgVal *val)
{
MppCfgIoImpl *impl = (MppCfgIoImpl *)obj;
if (!obj || !val || type >= MPP_CFG_TYPE_OBJECT) {
mpp_loge_f("invalid param obj %p val %p\n", obj, val);
return rk_nok;
}
if (impl->type != type) {
mpp_loge_f("obj %-16s type mismatch: expected %d, got %d\n",
impl->name, type, impl->type);
return rk_nok;
}
*val = impl->val;
return rk_ok;
}
rk_s32 mpp_cfg_to_struct(MppCfgObj obj, MppCfgObj type, void *st)
{
MppCfgIoImpl *orig;
+120
View File
@@ -11,6 +11,7 @@
#include "mpp_trie.h"
#include "mpp_common.h"
#include "mpp_cfg_io.h"
#include "mpp_enc_ref.h"
#include "mpp_enc_refs.h"
@@ -606,3 +607,122 @@ MppEncCpbInfo *mpp_enc_ref_cfg_get_cpb_info(MppEncRefCfg ref)
return &cfg->cpb_info;
}
/*
* JSON config apply / extract symmetric with mpp_enc_cfg_apply/extract
*/
MPP_RET mpp_enc_ref_cfg_apply(MppEncRefCfg ref, MppCfgStrFmt fmt, char *buf)
{
MppCfgObj root = NULL;
MppEncRefCfgImpl *impl;
MppCfgObj obj = NULL;
RK_S32 st_cnt = 0;
RK_S32 lt_cnt = 0;
MPP_RET ret = MPP_NOK;
if (!ref || !buf)
return MPP_NOK;
root = kmpp_objdef_get_cfg_root(mpp_enc_ref_cfg_def);
impl = (MppEncRefCfgImpl *)kmpp_obj_to_entry(ref);
if (mpp_cfg_from_string(&obj, fmt, buf) || !obj) {
mpp_loge_f("failed to parse config string\n");
mpp_cfg_put_all(obj);
return MPP_NOK;
}
/* read VLA capacity from parsed tree before to_struct */
{
MppCfgObj node = NULL;
char st_cnt_name[] = "st_cfg_cnt";
char lt_cnt_name[] = "lt_cfg_cnt";
MppCfgVal val = { 0 };
if (mpp_cfg_find(&node, obj, st_cnt_name, fmt) || !node) {
mpp_loge_f("failed to find st_cfg_cnt\n");
goto done;
}
if (mpp_cfg_get_val(node, MPP_CFG_TYPE_s32, &val)) {
mpp_loge_f("failed to read st_cfg_cnt\n");
goto done;
}
st_cnt = val.s32;
node = NULL;
if (mpp_cfg_find(&node, obj, lt_cnt_name, fmt) || !node) {
mpp_loge_f("failed to find lt_cfg_cnt\n");
goto done;
}
if (mpp_cfg_get_val(node, MPP_CFG_TYPE_s32, &val)) {
mpp_loge_f("failed to read lt_cfg_cnt\n");
goto done;
}
lt_cnt = val.s32;
}
if (st_cnt < 0 || lt_cnt < 0 ||
st_cnt > MPP_ENC_MAX_REF_CFG_NUM ||
lt_cnt > MPP_ENC_MAX_REF_CFG_NUM) {
mpp_loge_f("invalid count st %d lt %d (max %d)\n",
st_cnt, lt_cnt, MPP_ENC_MAX_REF_CFG_NUM);
goto done;
}
/* resize VLA to count from config */
if (st_cnt > impl->st_cfg_cap || lt_cnt > impl->lt_cfg_cap) {
if (ref_cfg_resize(impl, ref, lt_cnt, st_cnt, __FUNCTION__))
goto done;
impl = (MppEncRefCfgImpl *)kmpp_obj_to_entry(ref);
}
if (mpp_cfg_to_struct(obj, root, impl)) {
mpp_loge_f("failed to convert config to struct\n");
goto done;
}
ret = mpp_enc_ref_cfg_check(ref);
if (ret) {
mpp_loge_f("ref cfg check failed\n");
if (impl->st_cfg_cap > 0)
memset(MPP_REF_ST_ARR(impl), 0, impl->st_cfg_cap * sizeof(MppEncRefStFrmCfg));
if (impl->lt_cfg_cap > 0)
memset(MPP_REF_LT_ARR(impl), 0, impl->lt_cfg_cap * sizeof(MppEncRefLtFrmCfg));
impl->st_cfg_cnt = 0;
impl->lt_cfg_cnt = 0;
}
done:
mpp_cfg_put_all(obj);
return ret;
}
MPP_RET mpp_enc_ref_cfg_extract(MppEncRefCfg ref, MppCfgStrFmt fmt, char **buf)
{
MppEncRefCfgImpl *impl;
MppCfgObj root;
MppCfgObj obj = NULL;
rk_s32 ret;
if (!ref || !buf)
return MPP_NOK;
*buf = NULL;
impl = (MppEncRefCfgImpl *)kmpp_obj_to_entry(ref);
root = kmpp_objdef_get_cfg_root(mpp_enc_ref_cfg_def);
ret = mpp_cfg_from_struct(&obj, root, impl);
if (ret || !obj) {
mpp_loge_f("failed to convert struct to config\n");
return MPP_NOK;
}
ret = mpp_cfg_to_string(obj, fmt, buf);
if (ret)
mpp_loge_f("failed to convert config to string\n");
mpp_cfg_put_all(obj);
return *buf ? MPP_OK : MPP_NOK;
}