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