xref: /OK3568_Linux_fs/external/mpp/mpp/codec/inc/rc_data_impl.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2016 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 __RC_DATA_IMPL_H__
18 #define __RC_DATA_IMPL_H__
19 
20 #include "rc_data_base.h"
21 
22 typedef enum RcDataStatus_e {
23     RC_DATA_ST_UNUSED,
24     RC_DATA_ST_FILLING,
25     RC_DATA_ST_DONE,
26     RC_DATA_ST_BUTT,
27 } RcDataStatus;
28 
29 /*
30  * RcDataNode is composed of several parts:
31  *
32  * Sort function node   - for RcDataNode sorting
33  * Basic data node      - basic long-term data storage
34  * Extra data node      - extra short-term data storage
35  */
36 typedef struct RcDataNode_t RcDataNode;
37 
38 typedef struct RcDataHead_t {
39     // node pointer to RcDataNode
40     RcDataNode          *node;
41 
42     /* unique sequential id */
43     RK_S32              seq_id;
44     /* frame status parameter */
45     EncFrmStatus        frm_status;
46     /* slot index in storage */
47     RK_S32              slot_id;
48 
49     /* list head to sequence list */
50     struct list_head    seq;
51     /* list head to frame type list */
52     struct list_head    type;
53     /* list head to temporal id list */
54     struct list_head    tid;
55     /* list head to status list */
56     struct list_head    status;
57 
58     RcDataStatus        data_status;
59 } RcDataHead;
60 
61 typedef struct RcDataBase_t {
62     // node pointer to RcDataNode
63     RcDataHead          *head;
64 
65     RK_S32              stream_size;
66     RK_S32              qp_sum;
67 } RcDataBase;
68 
69 /*
70  * RcDataExtra is the short-term detail data storage.
71  *
72  * RC module will keep this frame data history for RcImplApi to implement
73  * different rate control strategy.
74  */
75 typedef struct RcDataExtra_t {
76     // node pointer to RcDataNode
77     RcDataHead          *head;
78 
79     RK_S32              target_bit;
80 
81     RcHalSet            set;
82     RcHalRet            ret;
83 } RcDataExtra;
84 
85 typedef struct RcDataIndexes_t {
86     /* list head to sequence list */
87     struct list_head    seq;
88     RK_S32              seq_cnt;
89     RK_S32              seq_new;
90     RK_S32              seq_old;
91 
92     /*
93      * list head to frame type list
94      * 0 - I frame
95      * 1 - P frame
96      */
97     struct list_head    type[2];
98     RK_S32              type_cnt[2];
99 
100     /*
101      * list head to temporal layer list
102      * 0 - layer 0
103      * 1 - layer 1
104      * 2 - layer 2
105      * 3 - layer 3
106      */
107     struct list_head    tid[4];
108     RK_S32              tid_cnt[4];
109 
110     /*
111      * list head to status list
112      * 0 - unused
113      * 1 - filling
114      * 2 - fill done
115      */
116     struct list_head    status[3];
117     RK_S32              status_cnt[4];
118 } RcDataIndexes;
119 
120 typedef struct RcDataNode_t {
121     RcDataHead          head;
122     RcDataBase          base;
123     RcDataExtra         *extra;
124 } RcDataNode;
125 
126 typedef struct DataGroupImpl_t {
127     Mutex               *lock;
128 
129     RK_S32              base_cnt;
130     RK_S32              extra_cnt;
131 
132     /* status list header for recording the node */
133     RcDataIndexes       indexes;
134 
135     // cache for current update
136     RcDataNode          *curr_node;
137 
138     NodeGroup           *node;
139     NodeGroup           *extra;
140 } DataGroupImpl;
141 
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145 
146 /* mpp rate control data internal update function */
147 MPP_RET rc_data_set_frm_status(RcDataNode *node, EncFrmStatus status);
148 MPP_RET rc_data_set_hal_set(RcDataNode *node, RcHalSet *set);
149 MPP_RET rc_data_set_hal_ret(RcDataNode *node, RcHalRet *ret);
150 
151 MPP_RET rc_data_group_init(DataGroupImpl *p, RK_S32 base_cnt, RK_S32 extra_cnt);
152 MPP_RET rc_data_group_deinit(DataGroupImpl *p);
153 MPP_RET rc_data_group_reset(DataGroupImpl *p);
154 
155 RcDataNode *rc_data_group_get_node_by_seq_id(DataGroupImpl *p, RK_S32 seq_id);
156 RcDataNode *rc_data_group_get_node_by_status(DataGroupImpl *p, RcDataStatus status, RK_S32 head);
157 /* put the node to the tail of next status */
158 void rc_data_group_put_node(DataGroupImpl *p, RcDataNode *node);
159 
160 /*
161  * rc data access helper function
162  *
163  * rc_data_get_next
164  * Get next rc data for current frame rc data storage
165  * equal to rc_data_group_get_node_by_status( unused, head )
166  *
167  * rc_data_get_curr_latest
168  * Get latest current encoding frame rc data storage.
169  * This function is for normal usage and reencoding.
170  * equal to rc_data_group_get_node_by_status( filling, tail )
171  *
172  * rc_data_get_curr_tail
173  * Get oldest current encoding frame rc data storage.
174  * equal to rc_data_group_get_node_by_status( filling, head )
175  *
176  * rc_data_get_last
177  * Get oldest encoded frame rc data storage.
178  * equal to rc_data_group_get_node_by_status( done, head )
179  *
180  * Overall status and stage diagram
181  *
182  * oldest                                                       latest
183  * +                                               +           +
184  * |last                                           |  current  | next
185  * +----------------------------------------------------------------->
186  * |                     done                      |  filling  |unused
187  * +head                                       tail+head   tail+head
188  *
189  * status transaction: unused -> filling -> done -> unused
190  */
191 RcData rc_data_get_next(DataGroup grp);
192 RcData rc_data_get_curr_latest(DataGroup grp);
193 RcData rc_data_get_curr_oldest(DataGroup grp);
194 RcData rc_data_get_last_latest(DataGroup grp);
195 RcData rc_data_get_last_oldest(DataGroup grp);
196 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #endif /* __RC_DATA_IMPL_H__ */
202