xref: /rockchip-linux_mpp/mpp/codec/enc/h264/h264e_dpb.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
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 __H264E_DPB_H__
18 #define __H264E_DPB_H__
19 
20 #include "h264e_syntax.h"
21 #include "h264e_sps.h"
22 #include "mpp_enc_ref.h"
23 
24 /*
25  * H.264 encoder dpb structure info
26  *
27  *     +----------+ DPB +-> build list +-> REF_LIST
28  *     |             +                        +
29  *     v             v                        v
30  * FRM_BUF_GRP    DPB_FRM +------+------> get_reorder
31  *     +             +           |
32  *     |             v           |
33  *     +--------> FRM_BUF        +------> get_mmco
34  *
35  * H264eDpb is overall structure contain the whole dpb info.
36  * It is composed H264eDpbFrm and H264eDpbList.
37  *
38  */
39 
40 #define MAX_GOP_SIZE                8
41 #define MAX_GOP_FMT_CNT             (MAX_GOP_SIZE+1)
42 #define MAX_GOP_FMT_SIZE            5
43 #define MAX_GOP_FMT_BUF_STUFF       16
44 #define MAX_GOP_FMT_BUF_SIZE        (MAX_GOP_FMT_CNT * MAX_GOP_FMT_SIZE + MAX_GOP_FMT_BUF_STUFF)
45 
46 #define H264E_ST_GOP_FLAG           (0x00000001)
47 #define H264E_ST_GOP_WITH_LT_REF    (0x00000002)
48 #define H264E_LT_GOP_FLAG           (0x00000010)
49 
50 #define REF_BY_RECN(idx)            (0x00000001 << idx)
51 #define REF_BY_REFR(idx)            (0x00000001 << idx)
52 
53 typedef struct  H264eDpbFrm_t {
54     RK_S32              slot_idx;
55     // frame index in frames
56     RK_S32              seq_idx;
57 
58     union {
59         RK_U32          on_used;
60         struct {
61             RK_U32      dpb_used    : 8;
62             RK_U32      hal_used    : 8;
63         };
64     };
65 
66     /* frame status */
67     EncFrmStatus        status;
68     RK_S32              prev_ref_idx;
69     RK_S32              as_pskip_ref;
70 
71     /* frame number from H264eSlice */
72     RK_S32              frame_num;
73     RK_S32              lt_idx;
74     /* poc from H264eSlice */
75     RK_S32              poc;
76     /* pts from input MppFrame */
77     RK_S64              pts;
78 } H264eDpbFrm;
79 
80 /* runtime status record */
81 typedef struct H264eDpbRt_t {
82     RK_S32              last_seq_idx;
83     RK_S32              last_is_ref;
84     RK_S32              last_frm_num;
85     RK_S32              last_poc_lsb;
86     RK_S32              last_poc_msb;
87 } H264eDpbRt;
88 
89 /*
90  * dpb frame arrangement
91  *
92  * If dpb size is 3 then dpb frame will be total 4 frames.
93  * Then the frame 3 is always current frame and frame 0~2 is reference frame
94  * in the gop structure.
95  *
96  * When one frame is encoded all it info will be moved to its gop position for
97  * next frame encoding.
98  */
99 typedef struct H264eDpb_t {
100     H264eReorderInfo    *reorder;
101     H264eMarkingInfo    *marking;
102 
103     MppEncCpbInfo       info;
104     RK_S32              next_max_lt_idx;
105     RK_S32              curr_max_lt_idx;
106     RK_S32              st_size;
107     RK_S32              lt_size;
108     RK_S32              used_size;
109 
110     RK_S32              dpb_size;
111     RK_S32              total_cnt;
112 
113     /* status on dpb rebuild is needed */
114     RK_S32              max_frm_num;
115     RK_S32              max_poc_lsb;
116     RK_S32              poc_type;
117 
118     RK_S32              last_frm_num;
119     RK_S32              last_poc_lsb;
120     RK_S32              last_poc_msb;
121 
122     H264eDpbFrm         *curr;
123     H264eDpbFrm         *refr;
124     H264eDpbFrm         *list[H264E_MAX_REFS_CNT];
125     H264eDpbFrm         *stref[H264E_MAX_REFS_CNT];
126     H264eDpbFrm         *ltref[H264E_MAX_REFS_CNT];
127     H264eDpbFrm         *map[H264E_MAX_REFS_CNT + 1];
128 
129     // frame storage
130     H264eDpbRt          rt;
131     H264eDpbRt          rt_bak;
132     H264eDpbFrm         frames[H264E_MAX_REFS_CNT + 1];
133     H264eDpbFrm         frm_bak[H264E_MAX_REFS_CNT + 1];
134 } H264eDpb;
135 
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 
140 MPP_RET h264e_dpb_init(H264eDpb *dpb, H264eReorderInfo *reorder, H264eMarkingInfo *marking);
141 MPP_RET h264e_dpb_deinit(H264eDpb *dpb);
142 
143 MPP_RET h264e_dpb_setup(H264eDpb *dpb, MppEncCfgSet* cfg, H264eSps *sps);
144 
145 /*
146  * Setup current frame config using flags
147  * This config function will be called before each frame is encoded:
148  *
149  * idr      - current frame is force to IDR or not
150  * lt_ref   - current frame is marked as longterm reference
151  */
152 MPP_RET h264e_dpb_proc(H264eDpb *dpb, EncCpbStatus *cpb);
153 
154 /*
155  * hal usage flag mark / unmark function
156  */
157 MPP_RET h264e_dpb_hal_start(H264eDpb *dpb, RK_S32 slot_idx);
158 MPP_RET h264e_dpb_hal_end(H264eDpb *dpb, RK_S32 slot_idx);
159 
160 void h264e_dpb_check(H264eDpb *dpb, EncCpbStatus *cpb);
161 
162 #define h264e_dpb_dump_frms(dpb) h264e_dpb_dump_frm(dpb, __FUNCTION__, __LINE__)
163 
164 void h264e_dpb_dump_frm(H264eDpb *dpb, const char *caller, RK_S32 line);
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* __H264E_DPB_H__ */
171