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 } RcCfg; 183 184 /* 185 * Different rate control strategy will be implemented by different API config 186 */ 187 typedef struct RcImplApi_t { 188 char *name; 189 MppCodingType type; 190 RK_U32 ctx_size; 191 192 MPP_RET (*init)(void *ctx, RcCfg *cfg); 193 MPP_RET (*deinit)(void *ctx); 194 195 MPP_RET (*check_drop)(void *ctx, EncRcTask *task); 196 MPP_RET (*check_reenc)(void *ctx, EncRcTask *task); 197 198 /* 199 * frm_start - frame level rate control frm_start. 200 * The EncRcTaskInfo will be output to hal for hardware to implement. 201 * frm_end - frame level rate control frm_end. 202 * The EncRcTaskInfo is returned for real quality and bitrate. 203 */ 204 MPP_RET (*frm_start)(void *ctx, EncRcTask *task); 205 MPP_RET (*frm_end)(void *ctx, EncRcTask *task); 206 207 /* 208 * hal_start - hardware level rate control start. 209 * The EncRcTaskInfo will be output to hal for hardware to implement. 210 * hal_end - hardware level rate control end. 211 * The EncRcTaskInfo is returned for real quality and bitrate. 212 */ 213 MPP_RET (*hal_start)(void *ctx, EncRcTask *task); 214 MPP_RET (*hal_end)(void *ctx, EncRcTask *task); 215 } RcImplApi; 216 217 /* 218 * structures for RC API register and query 219 */ 220 typedef struct RcApiBrief_t { 221 const char *name; 222 MppCodingType type; 223 } RcApiBrief; 224 225 typedef struct RcApiQueryAll_t { 226 /* input param for query */ 227 RcApiBrief *brief; 228 RK_S32 max_count; 229 230 /* output query count */ 231 RK_S32 count; 232 } RcApiQueryAll; 233 234 typedef struct RcApiQueryType_t { 235 /* input param for query */ 236 RcApiBrief *brief; 237 RK_S32 max_count; 238 MppCodingType type; 239 240 /* output query count */ 241 RK_S32 count; 242 } RcApiQueryType; 243 244 #ifdef __cplusplus 245 extern "C" { 246 #endif 247 248 MPP_RET rc_api_add(const RcImplApi *api); 249 MPP_RET rc_brief_get_all(RcApiQueryAll *query); 250 MPP_RET rc_brief_get_by_type(RcApiQueryType *query); 251 252 #ifdef __cplusplus 253 } 254 #endif 255 256 #endif /* __MPP_RC_API_H__ */ 257