1+ /* SPDX-License-Identifier: Apache-2.0 */
2+ /*
3+ * Copyright (c) 2026 Rockchip Electronics Co., Ltd.
4+ */
5+
6+ #include "hal_enc_task.h"
7+ #include "hal_h265e_vepu511a_reg.h"
8+
9+ typedef struct HalH265eVepu511aTune_t {
10+ H265eV511AHalContext * ctx ;
11+
12+ MppBuffer md_info ; /* internal md info buffer */
13+ RK_U8 * qm_mv_buf ; /* qpmap move flag buffer */
14+ RK_U32 qm_mv_buf_size ;
15+ } HalH265eVepu511aTune ;
16+
17+ static HalH265eVepu511aTune * vepu511a_h265e_tune_init (H265eV511AHalContext * ctx )
18+ {
19+ HalH265eVepu511aTune * tune = mpp_calloc (HalH265eVepu511aTune , 1 );
20+
21+ hal_h265e_dbg_func ("enter\n" );
22+
23+ if (NULL == tune )
24+ return tune ;
25+
26+ tune -> ctx = ctx ;
27+
28+ hal_h265e_dbg_func ("leave\n" );
29+
30+ return tune ;
31+ }
32+
33+ static void vepu511a_h265e_tune_deinit (void * tune )
34+ {
35+ HalH265eVepu511aTune * t = (HalH265eVepu511aTune * )tune ;
36+
37+ hal_h265e_dbg_func ("enter\n" );
38+
39+ if (t -> md_info ) {
40+ mpp_buffer_put (t -> md_info );
41+ t -> md_info = NULL ;
42+ }
43+
44+ MPP_FREE (t -> qm_mv_buf );
45+ MPP_FREE (tune );
46+ hal_h265e_dbg_func ("leave\n" );
47+ }
48+
49+ static MPP_RET vepu511a_h265e_tune_qpmap_init (HalH265eVepu511aTune * tune , MppBuffer md_info )
50+ {
51+ H265eV511AHalContext * ctx = tune -> ctx ;
52+ Vepu511aH265eFrmCfg * frm = ctx -> frm ;
53+ H265eV511ARegSet * regs = frm -> regs_set ;
54+ H265eVepu511aFrame * reg_frm = & regs -> reg_frm ;
55+ RK_S32 w32 = MPP_ALIGN (ctx -> cfg -> prep .width , 32 );
56+ RK_S32 h32 = MPP_ALIGN (ctx -> cfg -> prep .height , 32 );
57+ RK_S32 roir_buf_fd = -1 ;
58+
59+ hal_h265e_dbg_func ("enter\n" );
60+
61+ if (frm -> roi_data ) {
62+ //TODO: external qpmap buffer
63+ } else {
64+ if (NULL == frm -> roir_buf ) {
65+ if (NULL == ctx -> roi_grp )
66+ mpp_buffer_group_get_internal (& ctx -> roi_grp , MPP_BUFFER_TYPE_ION );
67+
68+ //TODO: bmap_mdc_dpth = 1 ???
69+ frm -> roir_buf_size = w32 * h32 / 256 * 4 ;
70+ mpp_buffer_get (ctx -> roi_grp , & frm -> roir_buf , frm -> roir_buf_size );
71+ }
72+
73+ roir_buf_fd = mpp_buffer_get_fd (frm -> roir_buf );
74+ }
75+
76+ if (NULL == frm -> roir_buf ) {
77+ mpp_err ("failed to get roir_buf\n" );
78+ return MPP_ERR_MALLOC ;
79+ }
80+ reg_frm -> common .adr_roir = roir_buf_fd ;
81+
82+ if (NULL == tune -> qm_mv_buf ) {
83+ tune -> qm_mv_buf_size = w32 * h32 / 256 ;
84+ tune -> qm_mv_buf = mpp_calloc (RK_U8 , tune -> qm_mv_buf_size );
85+ if (NULL == tune -> qm_mv_buf ) {
86+ mpp_err ("failed to get qm_mv_buf\n" );
87+ return MPP_ERR_MALLOC ;
88+ }
89+ }
90+
91+ if (NULL == md_info ) {
92+ RK_S32 meir_size = w32 * h32 / 256 * 4 ; /* max 4 bytes for each CU16 */
93+ if (NULL == tune -> md_info ) {
94+ mpp_buffer_get (NULL , & tune -> md_info , meir_size );
95+ }
96+
97+ if (tune -> md_info ) {
98+ reg_frm -> common .enc_pic .mei_stor = 1 ;
99+ reg_frm -> common .meiw_addr = mpp_buffer_get_fd (tune -> md_info );
100+ } else {
101+ reg_frm -> common .enc_pic .mei_stor = 0 ;
102+ reg_frm -> common .meiw_addr = 0 ;
103+ return MPP_ERR_MALLOC ;
104+ }
105+ hal_h265e_dbg_ctl ("md_info_internal %p, size %d\n" , tune -> md_info , meir_size );
106+ }
107+
108+ hal_h265e_dbg_ctl ("roir_buf_fd %d, size %d qm_mv_buf %p size %d\n" ,
109+ roir_buf_fd , frm -> roir_buf_size , tune -> qm_mv_buf ,
110+ tune -> qm_mv_buf_size );
111+ hal_h265e_dbg_func ("leave\n" );
112+
113+ return MPP_OK ;
114+ }
115+
116+ static void vepu511a_h265e_tune_qpmap (void * p , HalEncTask * task )
117+ {
118+ MPP_RET ret = MPP_OK ;
119+ HalH265eVepu511aTune * tune = (HalH265eVepu511aTune * )p ;
120+
121+ (void )task ;
122+ hal_h265e_dbg_func ("enter\n" );
123+
124+ ret = vepu511a_h265e_tune_qpmap_init (tune , task -> md_info );
125+ if (ret != MPP_OK ) {
126+ mpp_err ("failed to init qpmap\n" );
127+ return ;
128+ }
129+
130+ hal_h265e_dbg_func ("leave\n" );
131+ }
132+
133+ static void vepu511a_h265e_tune_reg_patch (void * p , HalEncTask * task )
134+ {
135+ HalH265eVepu511aTune * tune = (HalH265eVepu511aTune * )p ;
136+
137+ hal_h265e_dbg_func ("enter\n" );
138+
139+ if (NULL == tune )
140+ return ;
141+ H265eV511AHalContext * ctx = tune -> ctx ;
142+
143+ if (ctx -> qpmap_en ) {
144+ vepu511a_h265e_tune_qpmap (tune , task );
145+ }
146+
147+ hal_h265e_dbg_func ("leave\n" );
148+ }
0 commit comments