• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2015 The Android Open Source Project
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  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 
21 /**
22 *******************************************************************************
23 * @file
24 *  impeg2_buf_mgr.c
25 *
26 * @brief
27 *  Contains function definitions for buffer management
28 *
29 * @author
30 *  Srinivas T
31 *
32 * @par List of Functions:
33 *   - impeg2_buf_mgr_init()
34 *   - impeg2_buf_mgr_add()
35 *   - impeg2_buf_mgr_get_next_free()
36 *   - impeg2_buf_mgr_check_free()
37 *   - impeg2_buf_mgr_release()
38 *   - impeg2_buf_mgr_set_status()
39 *   - impeg2_buf_mgr_get_status()
40 *   - impeg2_buf_mgr_get_buf()
41 *   - impeg2_buf_mgr_get_num_active_buf()
42 *
43 * @remarks
44 *  None
45 *
46 *******************************************************************************
47 */
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include "iv_datatypedef.h"
51 #include "impeg2_defs.h"
52 #include "impeg2_buf_mgr.h"
53 
54 
55 
56 /**
57 *******************************************************************************
58 *
59 * @brief
60 *      Buffer manager initialization function.
61 *
62 * @par Description:
63 *    Initializes the buffer manager structure
64 *
65 * @param[in] ps_buf_mgr
66 *  Pointer to the buffer manager
67 *
68 * @returns
69 *
70 * @remarks
71 *  None
72 *
73 *******************************************************************************
74 */
75 
impeg2_buf_mgr_init(buf_mgr_t * ps_buf_mgr)76 void impeg2_buf_mgr_init(
77                 buf_mgr_t *ps_buf_mgr)
78 {
79     WORD32 id;
80 
81     ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
82     ps_buf_mgr->u4_active_buf_cnt = 0;
83 
84     for(id = 0; id < BUF_MGR_MAX_CNT; id++)
85     {
86         ps_buf_mgr->au4_status[id] = 0;
87         ps_buf_mgr->apv_ptr[id] = NULL;
88     }
89 }
90 
91 /**
92 *******************************************************************************
93 *
94 * @brief
95 *      Buffer manager reset function.
96 *
97 * @par Description:
98 *    Resets the buffer manager structure
99 *
100 * @param[in] ps_buf_mgr
101 *  Pointer to the buffer manager
102 *
103 * @returns
104 *
105 * @remarks
106 *  None
107 *
108 *******************************************************************************
109 */
110 
impeg2_buf_mgr_reset(buf_mgr_t * ps_buf_mgr)111 void impeg2_buf_mgr_reset(
112                 buf_mgr_t *ps_buf_mgr)
113 {
114     WORD32 id;
115 
116     ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
117     ps_buf_mgr->u4_active_buf_cnt = 0;
118 
119     for(id = 0; id < BUF_MGR_MAX_CNT; id++)
120     {
121         ps_buf_mgr->au4_status[id] = 0;
122     }
123 }
124 
125 /**
126 *******************************************************************************
127 *
128 * @brief
129 *       Adds and increments the buffer and buffer count.
130 *
131 * @par Description:
132 *     Adds a buffer to the buffer manager if it is not already  present and
133 *   increments the  active buffer count
134 *
135 * @param[in] ps_buf_mgr
136 *  Pointer to the buffer manager
137 *
138 * @param[in] pv_ptr
139 *  Pointer to the buffer to be added
140 *
141 * @returns  Returns 0 on success, -1 otherwise
142 *
143 * @remarks
144 *  None
145 *
146 *******************************************************************************
147 */
impeg2_buf_mgr_add(buf_mgr_t * ps_buf_mgr,void * pv_ptr,WORD32 i4_buf_id)148 WORD32 impeg2_buf_mgr_add(
149                 buf_mgr_t *ps_buf_mgr,
150                 void *pv_ptr,
151                 WORD32 i4_buf_id)
152 {
153 
154     /* Check if buffer ID is within allowed range */
155     if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
156     {
157         return (-1);
158     }
159 
160     /* Check if the current ID is being used to hold some other buffer */
161     if((ps_buf_mgr->apv_ptr[i4_buf_id] != NULL) &&
162        (ps_buf_mgr->apv_ptr[i4_buf_id] != pv_ptr))
163     {
164         return (-1);
165     }
166     ps_buf_mgr->apv_ptr[i4_buf_id] = pv_ptr;
167 
168     return 0;
169 }
170 
171 
172 /**
173 *******************************************************************************
174 *
175 * @brief
176 *   Gets the next free buffer.
177 *
178 * @par Description:
179 *     Returns the next free buffer available and sets the  corresponding status
180 *   to DEC
181 *
182 * @param[in] ps_buf_mgr
183 *  Pointer to the buffer manager
184 *
185 * @param[in] pi4_buf_id
186 *  Pointer to the id of the free buffer
187 *
188 * @returns  Pointer to the free buffer
189 *
190 * @remarks
191 *  None
192 *
193 *******************************************************************************
194 */
impeg2_buf_mgr_get_next_free(buf_mgr_t * ps_buf_mgr,WORD32 * pi4_buf_id)195 void* impeg2_buf_mgr_get_next_free(
196                 buf_mgr_t *ps_buf_mgr,
197                 WORD32 *pi4_buf_id)
198 {
199     WORD32 id;
200     void *pv_ret_ptr;
201 
202     pv_ret_ptr = NULL;
203     for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
204     {
205         /* Check if the buffer is non-null and status is zero */
206         if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
207         {
208             *pi4_buf_id = id;
209             /* DEC is set to 1 */
210             ps_buf_mgr->au4_status[id] = 1;
211             pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
212             break;
213         }
214     }
215 
216     return pv_ret_ptr;
217 }
218 
219 
220 /**
221 *******************************************************************************
222 *
223 * @brief
224 *      Checks the buffer manager for free buffers available.
225 *
226 * @par Description:
227 *  Checks if there are any free buffers available
228 *
229 * @param[in] ps_buf_mgr
230 *  Pointer to the buffer manager
231 *
232 * @returns  Returns 0 if available, -1 otherwise
233 *
234 * @remarks
235 *  None
236 *
237 *******************************************************************************
238 */
impeg2_buf_mgr_check_free(buf_mgr_t * ps_buf_mgr)239 WORD32 impeg2_buf_mgr_check_free(
240                 buf_mgr_t *ps_buf_mgr)
241 {
242     UWORD32 id;
243 
244     for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
245     {
246         if((ps_buf_mgr->au4_status[id] == 0) &&
247            (ps_buf_mgr->apv_ptr[id]))
248         {
249             return 1;
250         }
251     }
252 
253     return 0;
254 
255 }
256 
257 
258 /**
259 *******************************************************************************
260 *
261 * @brief
262 *       Resets the status bits.
263 *
264 * @par Description:
265 *     resets the status bits that the mask contains (status  corresponding to
266 *    the id)
267 *
268 * @param[in] ps_buf_mgr
269 *  Pointer to the buffer manager
270 *
271 * @param[in] buf_id
272 *  ID of the buffer status to be released
273 *
274 * @param[in] mask
275 *  Contains the bits that are to be reset
276 *
277 * @returns  0 if success, -1 otherwise
278 *
279 * @remarks
280 *  None
281 *
282 *******************************************************************************
283 */
impeg2_buf_mgr_release(buf_mgr_t * ps_buf_mgr,WORD32 i4_buf_id,UWORD32 u4_mask)284 WORD32 impeg2_buf_mgr_release(
285                 buf_mgr_t *ps_buf_mgr,
286                 WORD32 i4_buf_id,
287                 UWORD32 u4_mask)
288 {
289     /* If the given id is pointing to an id which is not yet added */
290     if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
291     {
292         return (-1);
293     }
294 
295     if(0 == (ps_buf_mgr->au4_status[i4_buf_id] & u4_mask))
296     {
297         return (-1);
298     }
299 
300     ps_buf_mgr->au4_status[i4_buf_id] &= ~u4_mask;
301 
302     /* If both the REF and DISP are zero, DEC is set to zero */
303     if(ps_buf_mgr->au4_status[i4_buf_id] == 1)
304     {
305         ps_buf_mgr->au4_status[i4_buf_id] = 0;
306     }
307 
308     return 0;
309 }
310 
311 
312 /**
313 *******************************************************************************
314 *
315 * @brief
316 *      Sets the status bit.
317 *
318 * @par Description:
319 *     sets the status bits that the mask contains (status  corresponding to the
320 *    id)
321 *
322 *
323 * @param[in] ps_buf_mgr
324 *  Pointer to the buffer manager
325 *
326 * @param[in] buf_id
327 *  ID of the buffer whose status needs to be modified
328 *
329 *
330 * @param[in] mask
331 *  Contains the bits that are to be set
332 *
333 * @returns  0 if success, -1 otherwise
334 *
335 * @remarks
336 *  None
337 *
338 *******************************************************************************
339 */
impeg2_buf_mgr_set_status(buf_mgr_t * ps_buf_mgr,WORD32 i4_buf_id,UWORD32 u4_mask)340 WORD32 impeg2_buf_mgr_set_status(
341                 buf_mgr_t *ps_buf_mgr,
342                 WORD32 i4_buf_id,
343                 UWORD32 u4_mask)
344 {
345     if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
346     {
347         return (-1);
348     }
349 
350 
351     if((ps_buf_mgr->au4_status[i4_buf_id] & u4_mask) != 0)
352     {
353         return (-1);
354     }
355 
356     ps_buf_mgr->au4_status[i4_buf_id] |= u4_mask;
357     return 0;
358 }
359 
360 
361 /**
362 *******************************************************************************
363 *
364 * @brief
365 *   Returns the status of the buffer.
366 *
367 * @par Description:
368 *  Returns the status of the buffer corresponding to the id
369 *
370 * @param[in] ps_buf_mgr
371 *  Pointer to the buffer manager
372 *
373 * @param[in] buf_id
374 *  ID of the buffer status required
375 *
376 * @returns  Status of the buffer corresponding to the id
377 *
378 * @remarks
379 *  None
380 *
381 *******************************************************************************
382 */
impeg2_buf_mgr_get_status(buf_mgr_t * ps_buf_mgr,WORD32 i4_buf_id)383 UWORD32 impeg2_buf_mgr_get_status(
384                 buf_mgr_t *ps_buf_mgr,
385                 WORD32 i4_buf_id)
386 {
387     return ps_buf_mgr->au4_status[i4_buf_id];
388 }
389 
390 
391 /**
392 *******************************************************************************
393 *
394 * @brief
395 *      Gets the buffer from the buffer manager
396 *
397 * @par Description:
398 *        Returns the pointer to the buffer corresponding to the id
399 *
400 * @param[in] ps_buf_mgr
401 *  Pointer to the buffer manager
402 *
403 * @param[in] buf_id
404 *  ID of the buffer required
405 *
406 * @returns  Pointer to the buffer required
407 *
408 * @remarks
409 *  None
410 *
411 *******************************************************************************
412 */
impeg2_buf_mgr_get_buf(buf_mgr_t * ps_buf_mgr,WORD32 i4_buf_id)413 void* impeg2_buf_mgr_get_buf(
414                 buf_mgr_t *ps_buf_mgr,
415                 WORD32 i4_buf_id)
416 {
417     return ps_buf_mgr->apv_ptr[i4_buf_id];
418 }
419 
420 
421 /**
422 *******************************************************************************
423 *
424 * @brief
425 *        Gets the no.of active buffer
426 *
427 * @par Description:
428 *      Return the number of active buffers in the buffer manager
429 *
430 * @param[in] ps_buf_mgr
431 *  Pointer to the buffer manager
432 *
433 * @returns  number of active buffers
434 *
435 * @remarks
436 *  None
437 *
438 *******************************************************************************
439 */
impeg2_buf_mgr_get_num_active_buf(buf_mgr_t * ps_buf_mgr)440 UWORD32 impeg2_buf_mgr_get_num_active_buf(
441                 buf_mgr_t *ps_buf_mgr)
442 {
443     return ps_buf_mgr->u4_max_buf_cnt;
444 }
445