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