xref: /rockchip-linux_mpp/inc/mpp_rc_api.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2016 Rockchip Electronics Co., Ltd.
4  */
5 
6 #ifndef __MPP_RC_API_H__
7 #define __MPP_RC_API_H__
8 
9 #include "mpp_err.h"
10 #include "rk_venc_rc.h"
11 #include "mpp_rc_defs.h"
12 
13 /*
14  * Mpp rate control has three parts:
15  *
16  * 1. MPI user config module
17  *    MppEncRcCfg structure is provided to user for overall rate control config
18  *    Mpp will receive MppEncRcCfg from user, check parameter and set it to
19  *    encoder.
20  *
21  * 2. Encoder rate control module
22  *    Encoder will implement the rate control strategy required by users
23  *    including CBR, VBR, AVBR and so on.
24  *    This module only implement the target bit calculation behavior and
25  *    quality restriction. And the quality level will be controlled by hal.
26  *
27  * 3. Hal rate control module
28  *    Hal will implement the rate control on hardware. Hal will calculate the
29  *    QP parameter for hardware according to the frame level target bit
30  *    specified by the encoder. And the report the real bitrate and quality to
31  *    encoder.
32  *
33  * The header defines the communication interfaces and structures used between
34  * MPI, encoder and hal.
35  */
36 
37 typedef enum RcMode_e {
38     RC_VBR,
39     RC_CBR,
40     RC_FIXQP,
41     RC_AVBR,
42     RC_SMT,
43     RC_CVBR,
44     RC_QVBR,
45     RC_SE,          // super encode mode
46     RC_MODE_BUTT,
47 } RcMode;
48 
49 typedef enum GopMode_e {
50     NORMAL_P,
51     SMART_P,
52 } GopMode;
53 
54 /*
55  * frame rate parameters have great effect on rate control
56  *
57  * fps_in_flex
58  * 0 - fix input frame rate
59  * 1 - variable input frame rate
60  *
61  * fps_in_num
62  * input frame rate numerator, if 0 then default 30
63  *
64  * fps_in_denom
65  * input frame rate denominator, if 0 then default 1
66  *
67  * fps_out_flex
68  * 0 - fix output frame rate
69  * 1 - variable output frame rate
70  *
71  * fps_out_num
72  * output frame rate numerator, if 0 then default 30
73  *
74  * fps_out_denom
75  * output frame rate denominator, if 0 then default 1
76  */
77 typedef struct RcFpsCfg_t {
78     RK_S32      fps_in_flex;
79     RK_S32      fps_in_num;
80     RK_S32      fps_in_denom;
81     RK_S32      fps_out_flex;
82     RK_S32      fps_out_num;
83     RK_S32      fps_out_denom;
84 } RcFpsCfg;
85 
86 typedef struct RcSuperframeCfg_t {
87     MppEncRcSuperFrameMode  super_mode;
88     RK_U32                  super_i_thd;
89     RK_U32                  super_p_thd;
90     MppEncRcPriority        rc_priority;
91 } RcSuperframeCfg;
92 
93 typedef struct RcDebreathCfg_t {
94     RK_U32      enable;
95     RK_U32      strength;
96 } RcDebreathCfg;
97 
98 typedef struct RcHierQPCfg_t {
99     RK_S32      hier_qp_en;
100     RK_S32      hier_qp_delta[4];
101     RK_S32      hier_frame_num[4];
102 } RcHierQPCfg;
103 
104 /*
105  * Control parameter from external config
106  *
107  * It will be updated on rc/prep/gopref config changed.
108  */
109 typedef struct RcCfg_s {
110     /* encode image size */
111     RK_S32      width;
112     RK_S32      height;
113 
114     /* Use rc_mode to find different api */
115     RcMode      mode;
116 
117     RcFpsCfg    fps;
118 
119     GopMode     gop_mode;
120     /* I frame gop len */
121     RK_S32      igop;
122     /* visual gop len */
123     RK_S32      vgop;
124 
125     /* bitrate parameter */
126     RK_S32      bps_min;
127     RK_S32      bps_target;
128     RK_S32      bps_max;
129     RK_S32      stats_time;
130 
131     /* max I frame bit ratio to P frame bit */
132     RK_S32      max_i_bit_prop;
133     RK_S32      min_i_bit_prop;
134     RK_S32      init_ip_ratio;
135     /* layer bitrate proportion */
136     RK_S32      layer_bit_prop[4];
137 
138     /* quality parameter */
139     RK_S32      init_quality;
140     RK_S32      max_quality;
141     RK_S32      min_quality;
142     RK_S32      max_i_quality;
143     RK_S32      min_i_quality;
144     RK_S32      i_quality_delta;
145     RK_S32      vi_quality_delta;
146     RK_S32      fqp_min_i;
147     RK_S32      fqp_min_p;
148     RK_S32      fqp_max_i;
149     RK_S32      fqp_max_p;
150     /* layer quality proportion */
151     RK_S32      layer_quality_delta[4];
152 
153     /* reencode parameter */
154     RK_S32      max_reencode_times;
155 
156     /* still / motion desision parameter */
157     RK_S32      min_still_prop;
158     RK_S32      max_still_quality;
159 
160     /*
161      * vbr parameter
162      *
163      * vbr_hi_prop  - high proportion bitrate for reduce quality
164      * vbr_lo_prop  - low proportion bitrate for increase quality
165      */
166     RK_S32      vbr_hi_prop;
167     RK_S32      vbr_lo_prop;
168 
169     MppEncRcDropFrmMode drop_mode;
170     RK_U32      drop_thd;
171     RK_U32      drop_gap;
172 
173     RcSuperframeCfg super_cfg;
174     RcDebreathCfg   debreath_cfg;
175     RcHierQPCfg     hier_qp_cfg;
176     RK_U32          refresh_len;
177     RK_S32          scene_mode;
178     RK_U32          fps_chg_prop;
179 
180     RK_S32          rc_container;
181 } RcCfg;
182 
183 /*
184  * Different rate control strategy will be implemented by different API config
185  */
186 typedef struct RcImplApi_t {
187     char            *name;
188     MppCodingType   type;
189     RK_U32          ctx_size;
190 
191     MPP_RET         (*init)(void *ctx, RcCfg *cfg);
192     MPP_RET         (*deinit)(void *ctx);
193 
194     MPP_RET         (*check_drop)(void *ctx, EncRcTask *task);
195     MPP_RET         (*check_reenc)(void *ctx, EncRcTask *task);
196 
197     /*
198      * frm_start -  frame level rate control frm_start.
199      *              The EncRcTaskInfo will be output to hal for hardware to implement.
200      * frm_end   -  frame level rate control frm_end.
201      *              The EncRcTaskInfo is returned for real quality and bitrate.
202      */
203     MPP_RET         (*frm_start)(void *ctx, EncRcTask *task);
204     MPP_RET         (*frm_end)(void *ctx, EncRcTask *task);
205 
206     /*
207      * hal_start -  hardware level rate control start.
208      *              The EncRcTaskInfo will be output to hal for hardware to implement.
209      * hal_end   -  hardware level rate control end.
210      *              The EncRcTaskInfo is returned for real quality and bitrate.
211      */
212     MPP_RET         (*hal_start)(void *ctx, EncRcTask *task);
213     MPP_RET         (*hal_end)(void *ctx, EncRcTask *task);
214 } RcImplApi;
215 
216 /*
217  * structures for RC API register and query
218  */
219 typedef struct RcApiBrief_t {
220     const char      *name;
221     MppCodingType   type;
222 } RcApiBrief;
223 
224 typedef struct RcApiQueryAll_t {
225     /* input param for query */
226     RcApiBrief      *brief;
227     RK_S32          max_count;
228 
229     /* output query count */
230     RK_S32          count;
231 } RcApiQueryAll;
232 
233 typedef struct RcApiQueryType_t {
234     /* input param for query */
235     RcApiBrief      *brief;
236     RK_S32          max_count;
237     MppCodingType   type;
238 
239     /* output query count */
240     RK_S32          count;
241 } RcApiQueryType;
242 
243 #ifdef __cplusplus
244 extern "C" {
245 #endif
246 
247 MPP_RET rc_api_add(const RcImplApi *api);
248 MPP_RET rc_brief_get_all(RcApiQueryAll *query);
249 MPP_RET rc_brief_get_by_type(RcApiQueryType *query);
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 #endif /* __MPP_RC_API_H__ */
256