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