xref: /OK3568_Linux_fs/external/mpp/mpp/codec/enc/h264/h264e_dpb.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
69     /* frame number from H264eSlice */
70     RK_S32              frame_num;
71     RK_S32              lt_idx;
72     /* poc from H264eSlice */
73     RK_S32              poc;
74     /* pts from input MppFrame */
75     RK_S64              pts;
76 } H264eDpbFrm;
77 
78 /* runtime status record */
79 typedef struct H264eDpbRt_t {
80     RK_S32              last_seq_idx;
81     RK_S32              last_is_ref;
82     RK_S32              last_frm_num;
83     RK_S32              last_poc_lsb;
84     RK_S32              last_poc_msb;
85 } H264eDpbRt;
86 
87 /*
88  * dpb frame arrangement
89  *
90  * If dpb size is 3 then dpb frame will be total 4 frames.
91  * Then the frame 3 is always current frame and frame 0~2 is reference frame
92  * in the gop structure.
93  *
94  * When one frame is encoded all it info will be moved to its gop position for
95  * next frame encoding.
96  */
97 typedef struct H264eDpb_t {
98     H264eReorderInfo    *reorder;
99     H264eMarkingInfo    *marking;
100 
101     MppEncCpbInfo       info;
102     RK_S32              next_max_lt_idx;
103     RK_S32              curr_max_lt_idx;
104     RK_S32              st_size;
105     RK_S32              lt_size;
106     RK_S32              used_size;
107 
108     RK_S32              dpb_size;
109     RK_S32              total_cnt;
110 
111     /* status on dpb rebuild is needed */
112     RK_S32              max_frm_num;
113     RK_S32              max_poc_lsb;
114     RK_S32              poc_type;
115 
116     RK_S32              last_frm_num;
117     RK_S32              last_poc_lsb;
118     RK_S32              last_poc_msb;
119 
120     H264eDpbFrm         *curr;
121     H264eDpbFrm         *refr;
122     H264eDpbFrm         *list[H264E_MAX_REFS_CNT];
123     H264eDpbFrm         *stref[H264E_MAX_REFS_CNT];
124     H264eDpbFrm         *ltref[H264E_MAX_REFS_CNT];
125     H264eDpbFrm         *map[H264E_MAX_REFS_CNT + 1];
126 
127     // frame storage
128     H264eDpbRt          rt;
129     H264eDpbRt          rt_bak;
130     H264eDpbFrm         frames[H264E_MAX_REFS_CNT + 1];
131     H264eDpbFrm         frm_bak[H264E_MAX_REFS_CNT + 1];
132 } H264eDpb;
133 
134 #ifdef __cplusplus
135 extern "C" {
136 #endif
137 
138 MPP_RET h264e_dpb_init(H264eDpb *dpb, H264eReorderInfo *reorder, H264eMarkingInfo *marking);
139 MPP_RET h264e_dpb_deinit(H264eDpb *dpb);
140 
141 MPP_RET h264e_dpb_setup(H264eDpb *dpb, MppEncCfgSet* cfg, H264eSps *sps);
142 
143 /*
144  * Setup current frame config using flags
145  * This config function will be called before each frame is encoded:
146  *
147  * idr      - current frame is force to IDR or not
148  * lt_ref   - current frame is marked as longterm reference
149  */
150 MPP_RET h264e_dpb_proc(H264eDpb *dpb, EncCpbStatus *cpb);
151 
152 /*
153  * hal usage flag mark / unmark function
154  */
155 MPP_RET h264e_dpb_hal_start(H264eDpb *dpb, RK_S32 slot_idx);
156 MPP_RET h264e_dpb_hal_end(H264eDpb *dpb, RK_S32 slot_idx);
157 
158 void h264e_dpb_check(H264eDpb *dpb, EncCpbStatus *cpb);
159 
160 #define h264e_dpb_dump_frms(dpb) h264e_dpb_dump_frm(dpb, __FUNCTION__, __LINE__)
161 
162 void h264e_dpb_dump_frm(H264eDpb *dpb, const char *caller, RK_S32 line);
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* __H264E_DPB_H__ */
169