• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __RK_VENC_REF_H__
17 #define __RK_VENC_REF_H__
18 
19 #include "rk_type.h"
20 #include "mpp_err.h"
21 
22 /*
23  * MPP reference management system follows the model of H.264/H.265 reference
24  * frame mangement.
25  *
26  * The reference frame is defined into two type: long-term reference frame and
27  * short-refernce frame (lt_ref and st_ref).
28  *
29  * The lt_ref can be only indexed by long-term reference frame index (lt_idx).
30  * The st_ref can be indexed by its temporal id (tid) and previous count.
31  *
32  * MppEncRefMode defined the way for user to reference the required frame.
33  *
34  * Normal reference mode without argument
35  *  REF_TO_PREV_REF_FRM   - refer to previous reference frame in encode order (No matter Lt or St)
36  *  REF_TO_PREV_ST_REF    - refer to previous short-term reference frame
37  *  REF_TO_PREV_LT_REF    - refer to previous long-term reference frame
38  *  REF_TO_PREV_INTRA     - refer to previous Intra / IDR frame
39  *  REF_TO_ST_REF_SETUP   - refer to refernce frame defined in StRefSetup
40  *
41  * Normal reference mode with argument
42  *  REF_TO_TEMPORAL_LAYER - refer to previous reference frame with temporal id argument
43  *  REF_TO_LT_REF_IDX     - refer to long-term reference frame with lt_ref_idx argument
44  *  REF_TO_ST_PREV_N_REF    - refer to short-term reference frame with diff frame_num argument
45  *
46  * Long-term reference only mode
47  *  REF_TO_ST_REF_SETUP   - use corresponding mode of original short-term reference frame
48  *
49  * Short-term reference only mode
50  *  REF_TO_LT_REF_SETUP   - indicate that this frame will be overwrited by long-term config
51  *
52  * By combining frames with these modes user can define many kinds of reference hierarchy
53  * structure. But normally user should use simplified preset hierarchy pattern.
54  *
55  * The rules for virtual cpb management is similiar to H.264/H.265
56  * 1. When one frame is marked as long-term reference frame it will be kept in cpb until
57  *    it is replaced by other frame with the same lt_idx or IDR frame.
58  * 2. When one frame is marked as short-term reference frame it will be inert into cpb when
59  *    there is enough storage space. When the number of total sum of long-term and short-term
60  *    reference frame excess the cpb size limit the oldest short-term frame will be removed.
61  *    This is call sliding window in H.264.
62  */
63 
64 /* max 4 temporal layer */
65 #define MPP_ENC_MAX_TEMPORAL_LAYER_NUM      4
66 /* max 4 long-term reference frame */
67 #define MPP_ENC_MAX_LT_REF_NUM              16
68 
69 /*
70  * Group Of Picture (GOP) config is separated into three parts:
71  *
72  * 1. Intra / IDR frame config
73  *    igop  - the interval of two intra / IDR frames
74  *
75  * 2. Long-term reference config (MppEncRefLtFrmCfg)
76  *
77  *    Setup long-term reference index max lt_idx, loop interval and reference
78  *    mode for auto long-term reference frame generation. The encoder will
79  *    mark frame to be long-term reference frame with given interval.
80  *
81  *    2.1 lt_idx
82  *    The long-term reference frame index is unique identifier for a long-term
83  *    reference frame.
84  *    The max long-term reference frame index should NOT larger than
85  *    max_num_ref_frames in sps.
86  *
87  *    2.2 lt_gap
88  *    When lt_gap is zero the long-term reference frame generation is disabled.
89  *    When lt_gap is non-zero (usually 2~3 second interval) then the long-term
90  *    reference frame will be generated for error recovery or smart hierarchy.
91  *
92  *    2.2 lt_delay
93  *    The lt_delay is the delay time for generation of long-term reference frame.
94  *    The start point of lt_delay is the IDR/intra frame genertaed by igop.
95  *
96  *    2.4 ref_mode: Long-term refernce frame reference mode
97  *    NOTE: temporal id of longterm reference frame is always zero.
98  *
99  *    Examples:
100  *    Sequence has only one lt_ref 0 and setup one long-term reference frame
101  *    every 300 frame.
102  *    {
103  *    .lt_idx       = 0,
104  *    .lt_gap       = 300,
105  *    .lt_delay     = 0,
106  *    }
107  *    result:
108  *    frame   0 ...... 299 300 301 ...... 599 600 601
109  *    lt_idx  0 xxxxxx  x   0   x  xxxxxx  x   0   x
110  *
111  *    Sequence has lt_ref from 0 to 2 and setup a long-term reference frame
112  *    every 100 frame.
113  *    {
114  *    .lt_idx       = 0,
115  *    .lt_gap       = 300,
116  *    .lt_delay     = 0,
117  *    }
118  *    {
119  *    .lt_idx       = 1,
120  *    .lt_gap       = 300,
121  *    .lt_delay     = 100,
122  *    }
123  *    {
124  *    .lt_idx       = 2,
125  *    .lt_gap       = 300,
126  *    .lt_delay     = 200,
127  *    }
128  *    result:
129  *    frame   0 ... 99 100 101 ... 199 200 201 ... 299 300 301
130  *    lt_idx  0 xxx  x  1   x  xxx  x   2   x  xxx  x   0   x
131  *
132  * 3. Short-term reference config (MppEncStRefSetup)
133  *
134  *    3.1 is_non_ref
135  *    The is_non_ref indicated the current frame is reference frame or not.
136  *
137  *    3.2 temporal_id
138  *    The temporal id of the current frame configure.
139  *
140  *    3.3 ref_mode: short-term refernce frame reference mode
141  *
142  *    3.4 repeat
143  *    The repeat time of the short-term reference frame configure.
144  *    The overall frame count with the same config is repeat + 1.
145  *
146  *    Examples:
147  *
148  */
149 
150 #define REF_MODE_MODE_MASK              (0x1F)
151 #define REF_MODE_ARG_MASK               (0xFFFF0000)
152 
153 typedef enum MppEncRefMode_e {
154     /* max 32 mode in 32-bit */
155     /* for default ref global config */
156     REF_MODE_GLOBAL,
157     REF_TO_PREV_REF_FRM                 = REF_MODE_GLOBAL,
158     REF_TO_PREV_ST_REF,
159     REF_TO_PREV_LT_REF,
160     REF_TO_PREV_INTRA,
161 
162     /* for global config with args */
163     REF_MODE_GLOBAL_WITH_ARG            = 0x4,
164     /* with ref arg as temporal layer id */
165     REF_TO_TEMPORAL_LAYER               = REF_MODE_GLOBAL_WITH_ARG,
166     /* with ref arg as long-term reference picture index */
167     REF_TO_LT_REF_IDX,
168     /* with ref arg as short-term reference picture difference frame_num */
169     REF_TO_ST_PREV_N_REF,
170     REF_MODE_GLOBAL_BUTT,
171 
172     /* for lt-ref */
173     REF_MODE_LT                         = 0x18,
174     REF_TO_ST_REF_SETUP,
175     REF_MODE_LT_BUTT,
176 
177     /* for st-ref */
178     REF_MODE_ST                         = 0x1C,
179     REF_TO_LT_REF_SETUP,
180     REF_MODE_ST_BUTT,
181 } MppEncRefMode;
182 
183 typedef struct MppEncRefLtFrmCfg_t {
184     RK_S32              lt_idx;         /* lt_idx of the reference frame */
185     RK_S32              temporal_id;    /* temporal_id of the reference frame */
186     MppEncRefMode       ref_mode;
187     RK_S32              ref_arg;
188     RK_S32              lt_gap;         /* gap between two lt-ref with same lt_idx */
189     RK_S32              lt_delay;       /* delay offset to igop start frame */
190 } MppEncRefLtFrmCfg;
191 
192 typedef struct MppEncRefStFrmCfg_t {
193     RK_S32              is_non_ref;
194     RK_S32              temporal_id;
195     MppEncRefMode       ref_mode;
196     RK_S32              ref_arg;
197     RK_S32              repeat;         /* repeat times */
198 } MppEncRefStFrmCfg;
199 
200 typedef struct MppEncRefPreset_t {
201     /* input parameter for query */
202     const char          *name;
203     RK_S32              max_lt_cnt;
204     RK_S32              max_st_cnt;
205     MppEncRefLtFrmCfg   *lt_cfg;
206     MppEncRefStFrmCfg   *st_cfg;
207 
208     /* output parameter */
209     RK_S32              lt_cnt;
210     RK_S32              st_cnt;
211 } MppEncRefPreset;
212 
213 typedef void* MppEncRefCfg;
214 
215 #ifdef __cplusplus
216 extern "C" {
217 #endif
218 
219 MPP_RET mpp_enc_ref_cfg_init(MppEncRefCfg *ref);
220 MPP_RET mpp_enc_ref_cfg_deinit(MppEncRefCfg *ref);
221 
222 MPP_RET mpp_enc_ref_cfg_reset(MppEncRefCfg ref);
223 MPP_RET mpp_enc_ref_cfg_set_cfg_cnt(MppEncRefCfg ref, RK_S32 lt_cnt, RK_S32 st_cnt);
224 MPP_RET mpp_enc_ref_cfg_add_lt_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefLtFrmCfg *frm);
225 MPP_RET mpp_enc_ref_cfg_add_st_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefStFrmCfg *frm);
226 MPP_RET mpp_enc_ref_cfg_check(MppEncRefCfg ref);
227 
228 /*
229  * A new reference configure will restart a new gop and clear cpb by default.
230  * The keep cpb function will let encoder keeps the current cpb status and do NOT
231  * reset all the reference frame in cpb.
232  */
233 MPP_RET mpp_enc_ref_cfg_set_keep_cpb(MppEncRefCfg ref, RK_S32 keep);
234 MPP_RET mpp_enc_ref_cfg_get_preset(MppEncRefPreset *preset);
235 MPP_RET mpp_enc_ref_cfg_show(MppEncRefCfg ref);
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif /* __RK_VENC_REF_H__ */