1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /**
19 *******************************************************************************
20 * @file
21 * ihevc_buf_mgr.c
22 *
23 * @brief
24 * Contains function definitions for buffer management
25 *
26 * @author
27 * Srinivas T
28 *
29 * @par List of Functions:
30 * - ihevc_buf_mgr_init()
31 * - ihevc_buf_mgr_add()
32 * - ihevc_buf_mgr_get_next_free()
33 * - ihevc_buf_mgr_check_free()
34 * - ihevc_buf_mgr_release()
35 * - ihevc_buf_mgr_set_status()
36 * - ihevc_buf_mgr_get_status()
37 * - ihevc_buf_mgr_get_buf()
38 * - ihevc_buf_mgr_get_num_active_buf()
39 *
40 * @remarks
41 * None
42 *
43 *******************************************************************************
44 */
45 #include <stdlib.h>
46 #include "ihevc_typedefs.h"
47 #include "ihevc_macros.h"
48 #include "ihevc_func_selector.h"
49 #include "ihevc_buf_mgr.h"
50
51
52 /**
53 *******************************************************************************
54 *
55 * @brief
56 * Buffer manager initialization function.
57 *
58 * @par Description:
59 * Initializes the buffer manager structure
60 *
61 * @param[in] ps_buf_mgr
62 * Pointer to the buffer manager
63 *
64 * @returns
65 *
66 * @remarks
67 * None
68 *
69 *******************************************************************************
70 */
71
ihevc_buf_mgr_init(buf_mgr_t * ps_buf_mgr)72 void ihevc_buf_mgr_init(
73 buf_mgr_t *ps_buf_mgr)
74 {
75 WORD32 id;
76
77 ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
78 ps_buf_mgr->u4_active_buf_cnt = 0;
79
80 for(id = 0; id < BUF_MGR_MAX_CNT; id++)
81 {
82 ps_buf_mgr->au4_status[id] = 0;
83 ps_buf_mgr->apv_ptr[id] = NULL;
84 }
85 }
86
87
88 /**
89 *******************************************************************************
90 *
91 * @brief
92 * Adds and increments the buffer and buffer count.
93 *
94 * @par Description:
95 * Adds a buffer to the buffer manager if it is not already present and
96 * increments the active buffer count
97 *
98 * @param[in] ps_buf_mgr
99 * Pointer to the buffer manager
100 *
101 * @param[in] pv_ptr
102 * Pointer to the buffer to be added
103 *
104 * @returns Returns 0 on success, -1 otherwise
105 *
106 * @remarks
107 * None
108 *
109 *******************************************************************************
110 */
ihevc_buf_mgr_add(buf_mgr_t * ps_buf_mgr,void * pv_ptr,WORD32 buf_id)111 WORD32 ihevc_buf_mgr_add(
112 buf_mgr_t *ps_buf_mgr,
113 void *pv_ptr,
114 WORD32 buf_id)
115 {
116
117 /* Check if buffer ID is within allowed range */
118 if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
119 {
120 return (-1);
121 }
122
123 /* Check if the current ID is being used to hold some other buffer */
124 if((ps_buf_mgr->apv_ptr[buf_id] != NULL) &&
125 (ps_buf_mgr->apv_ptr[buf_id] != pv_ptr))
126 {
127 return (-1);
128 }
129 ps_buf_mgr->apv_ptr[buf_id] = pv_ptr;
130
131 return 0;
132 }
133
134
135 /**
136 *******************************************************************************
137 *
138 * @brief
139 * Gets the next free buffer.
140 *
141 * @par Description:
142 * Returns the next free buffer available and sets the corresponding status
143 * to DEC
144 *
145 * @param[in] ps_buf_mgr
146 * Pointer to the buffer manager
147 *
148 * @param[in] pi4_buf_id
149 * Pointer to the id of the free buffer
150 *
151 * @returns Pointer to the free buffer
152 *
153 * @remarks
154 * None
155 *
156 *******************************************************************************
157 */
ihevc_buf_mgr_get_next_free(buf_mgr_t * ps_buf_mgr,WORD32 * pi4_buf_id)158 void* ihevc_buf_mgr_get_next_free(
159 buf_mgr_t *ps_buf_mgr,
160 WORD32 *pi4_buf_id)
161 {
162 WORD32 id;
163 void *pv_ret_ptr;
164
165 pv_ret_ptr = NULL;
166 for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
167 {
168 /* Check if the buffer is non-null and status is zero */
169 if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
170 {
171 *pi4_buf_id = id;
172 /* DEC is set to 1 */
173 ps_buf_mgr->au4_status[id] = 1;
174 pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
175 break;
176 }
177 }
178
179 return pv_ret_ptr;
180 }
181
182
183 /**
184 *******************************************************************************
185 *
186 * @brief
187 * Checks the buffer manager for free buffers available.
188 *
189 * @par Description:
190 * Checks if there are any free buffers available
191 *
192 * @param[in] ps_buf_mgr
193 * Pointer to the buffer manager
194 *
195 * @returns Returns 0 if available, -1 otherwise
196 *
197 * @remarks
198 * None
199 *
200 *******************************************************************************
201 */
ihevc_buf_mgr_check_free(buf_mgr_t * ps_buf_mgr)202 WORD32 ihevc_buf_mgr_check_free(
203 buf_mgr_t *ps_buf_mgr)
204 {
205 UWORD32 id;
206
207 for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
208 {
209 if((ps_buf_mgr->au4_status[id] == 0) &&
210 (ps_buf_mgr->apv_ptr[id]))
211 {
212 return 1;
213 }
214 }
215
216 return 0;
217
218 }
219
220
221 /**
222 *******************************************************************************
223 *
224 * @brief
225 * Resets the status bits.
226 *
227 * @par Description:
228 * resets the status bits that the mask contains (status corresponding to
229 * the id)
230 *
231 * @param[in] ps_buf_mgr
232 * Pointer to the buffer manager
233 *
234 * @param[in] buf_id
235 * ID of the buffer status to be released
236 *
237 * @param[in] mask
238 * Contains the bits that are to be reset
239 *
240 * @returns 0 if success, -1 otherwise
241 *
242 * @remarks
243 * None
244 *
245 *******************************************************************************
246 */
ihevc_buf_mgr_release(buf_mgr_t * ps_buf_mgr,WORD32 buf_id,UWORD32 mask)247 WORD32 ihevc_buf_mgr_release(
248 buf_mgr_t *ps_buf_mgr,
249 WORD32 buf_id,
250 UWORD32 mask)
251 {
252 /* If the given id is pointing to an id which is not yet added */
253 if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
254 {
255 return (-1);
256 }
257
258 ps_buf_mgr->au4_status[buf_id] &= ~mask;
259
260 /* If both the REF and DISP are zero, DEC is set to zero */
261 if(ps_buf_mgr->au4_status[buf_id] == 1)
262 {
263 ps_buf_mgr->au4_status[buf_id] = 0;
264 }
265
266 return 0;
267 }
268
269
270 /**
271 *******************************************************************************
272 *
273 * @brief
274 * Sets the status bit.
275 *
276 * @par Description:
277 * sets the status bits that the mask contains (status corresponding to the
278 * id)
279 *
280 *
281 * @param[in] ps_buf_mgr
282 * Pointer to the buffer manager
283 *
284 * @param[in] buf_id
285 * ID of the buffer whose status needs to be modified
286 *
287 *
288 * @param[in] mask
289 * Contains the bits that are to be set
290 *
291 * @returns 0 if success, -1 otherwise
292 *
293 * @remarks
294 * None
295 *
296 *******************************************************************************
297 */
ihevc_buf_mgr_set_status(buf_mgr_t * ps_buf_mgr,WORD32 buf_id,UWORD32 mask)298 WORD32 ihevc_buf_mgr_set_status(
299 buf_mgr_t *ps_buf_mgr,
300 WORD32 buf_id,
301 UWORD32 mask)
302 {
303 if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
304 {
305 return (-1);
306 }
307
308
309 if((ps_buf_mgr->au4_status[buf_id] & mask) != 0)
310 {
311 return (-1);
312 }
313
314 ps_buf_mgr->au4_status[buf_id] |= mask;
315 return 0;
316 }
317
318
319 /**
320 *******************************************************************************
321 *
322 * @brief
323 * Returns the status of the buffer.
324 *
325 * @par Description:
326 * Returns the status of the buffer corresponding to the id
327 *
328 * @param[in] ps_buf_mgr
329 * Pointer to the buffer manager
330 *
331 * @param[in] buf_id
332 * ID of the buffer status required
333 *
334 * @returns Status of the buffer corresponding to the id
335 *
336 * @remarks
337 * None
338 *
339 *******************************************************************************
340 */
ihevc_buf_mgr_get_status(buf_mgr_t * ps_buf_mgr,WORD32 buf_id)341 UWORD32 ihevc_buf_mgr_get_status(
342 buf_mgr_t *ps_buf_mgr,
343 WORD32 buf_id)
344 {
345 return ps_buf_mgr->au4_status[buf_id];
346 }
347
348
349 /**
350 *******************************************************************************
351 *
352 * @brief
353 * Gets the buffer from the buffer manager
354 *
355 * @par Description:
356 * Returns the pointer to the buffer corresponding to the id
357 *
358 * @param[in] ps_buf_mgr
359 * Pointer to the buffer manager
360 *
361 * @param[in] buf_id
362 * ID of the buffer required
363 *
364 * @returns Pointer to the buffer required
365 *
366 * @remarks
367 * None
368 *
369 *******************************************************************************
370 */
ihevc_buf_mgr_get_buf(buf_mgr_t * ps_buf_mgr,WORD32 buf_id)371 void* ihevc_buf_mgr_get_buf(
372 buf_mgr_t *ps_buf_mgr,
373 WORD32 buf_id)
374 {
375 return ps_buf_mgr->apv_ptr[buf_id];
376 }
377
378
379 /**
380 *******************************************************************************
381 *
382 * @brief
383 * Gets the no.of active buffer
384 *
385 * @par Description:
386 * Return the number of active buffers in the buffer manager
387 *
388 * @param[in] ps_buf_mgr
389 * Pointer to the buffer manager
390 *
391 * @returns number of active buffers
392 *
393 * @remarks
394 * None
395 *
396 *******************************************************************************
397 */
ihevc_buf_mgr_get_num_active_buf(buf_mgr_t * ps_buf_mgr)398 UWORD32 ihevc_buf_mgr_get_num_active_buf(
399 buf_mgr_t *ps_buf_mgr)
400 {
401 return ps_buf_mgr->u4_max_buf_cnt;
402 }
403