aboutsummaryrefslogtreecommitdiff
path: root/common/ih264_buf_mgr.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/ih264_buf_mgr.c')
-rw-r--r--common/ih264_buf_mgr.c304
1 files changed, 139 insertions, 165 deletions
diff --git a/common/ih264_buf_mgr.c b/common/ih264_buf_mgr.c
index 0b5a596..a6c66d0 100644
--- a/common/ih264_buf_mgr.c
+++ b/common/ih264_buf_mgr.c
@@ -17,6 +17,7 @@
*****************************************************************************
* Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
+
/**
*******************************************************************************
* @file
@@ -26,53 +27,65 @@
* Contains function definitions for buffer management
*
* @author
-* Srinivas T
+* ittiam
*
* @par List of Functions:
-* - ih264_buf_mgr_size()
-* - ih264_buf_mgr_lock()
-* - ih264_buf_mgr_unlock()
-* - ih264_buf_mgr_yield()
-* - ih264_buf_mgr_free()
-* - ih264_buf_mgr_init()
-* - ih264_buf_mgr_add()
-* - ih264_buf_mgr_get_next_free()
-* - ih264_buf_mgr_check_free()
-* - ih264_buf_mgr_set_status()
-* - ih264_buf_mgr_get_status()
-* - ih264_buf_mgr_get_buf()
-* - ih264_buf_mgr_get_bufid()
-* - ih264_buf_mgr_get_num_active_buf()
+* - ih264_buf_mgr_size
+* - ih264_buf_mgr_lock
+* - ih264_buf_mgr_unlock
+* - ih264_buf_mgr_yield
+* - ih264_buf_mgr_free
+* - ih264_buf_mgr_init
+* - ih264_buf_mgr_add
+* - ih264_buf_mgr_get_next_free
+* - ih264_buf_mgr_check_free
+* - ih264_buf_mgr_release
+* - ih264_buf_mgr_set_status
+* - ih264_buf_mgr_get_status
+* - ih264_buf_mgr_get_buf
+* - ih264_buf_mgr_get_bufid
+* - ih264_buf_mgr_get_num_active_buf
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
+
+/*****************************************************************************/
+/* File Includes */
+/*****************************************************************************/
+
+/* System Include Files */
#include <stdio.h>
#include <stdlib.h>
+
+/* User Include Files */
#include "ih264_typedefs.h"
+#include "ithread.h"
#include "ih264_macros.h"
-#include "ih264_defs.h"
#include "ih264_error.h"
+#include "ih264_defs.h"
#include "ih264_buf_mgr.h"
-#include "ithread.h"
+
+/*****************************************************************************/
+/* Function Definitions */
+/*****************************************************************************/
/**
*******************************************************************************
*
-* @brief Returns size for buf queue context. Does not include buf queue buffer
-* requirements
+* @brief Returns size for buffer queue context. Does not include the size of the
+* actual buffers that are stored here.
*
-* @par Description
-* Returns size for buf queue context. Does not include buf queue buffer
-* requirements. Buffer size required to store the bufs should be allocated in
-* addition to the value returned here.
+* @par Description
+* Returns size for buf queue context. Does not include the size of the
+* actual buffers that are stored here.
*
* @returns Size of the buf queue context
*
-* @remarks
+* @remarks none
*
*******************************************************************************
*/
@@ -89,43 +102,40 @@ WORD32 ih264_buf_mgr_size(void)
/**
*******************************************************************************
*
-* @brief
-* Locks the buf_mgr context
+* @brief Locks the buffer manager context
*
-* @par Description
-* Locks the buf_mgr context by calling ithread_mutex_lock()
+* @par Description
+* Locks the buffer manager context by calling ithread_mutex_lock()
*
* @param[in] ps_buf_mgr
-* Job Queue context
+* Pointer to the buffer manager
*
* @returns IH264_FAIL if mutex lock fails else IH264_SUCCESS
*
-* @remarks
+* @remarks none
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_lock(buf_mgr_t *ps_buf_mgr)
{
WORD32 retval;
+
retval = ithread_mutex_lock(ps_buf_mgr->pv_mutex);
if(retval)
- {
return IH264_FAIL;
- }
return IH264_SUCCESS;
}
/**
*******************************************************************************
*
-* @brief
-* Unlocks the buf_mgr context
+* @brief Unlocks the buffer manager context
*
-* @par Description
-* Unlocks the buf_mgr context by calling ithread_mutex_unlock()
+* @par Description
+* Unlocks the buffer manager context by calling ithread_mutex_unlock()
*
* @param[in] ps_buf_mgr
-* Job Queue context
+* Pointer to the buffer manager
*
* @returns IH264_FAIL if mutex unlock fails else IH264_SUCCESS
*
@@ -133,33 +143,30 @@ IH264_ERROR_T ih264_buf_mgr_lock(buf_mgr_t *ps_buf_mgr)
*
*******************************************************************************
*/
-
IH264_ERROR_T ih264_buf_mgr_unlock(buf_mgr_t *ps_buf_mgr)
{
WORD32 retval;
+
retval = ithread_mutex_unlock(ps_buf_mgr->pv_mutex);
if(retval)
- {
return IH264_FAIL;
- }
return IH264_SUCCESS;
-
}
+
/**
*******************************************************************************
*
-* @brief
-* Yeilds the thread
+* @brief Yields the thread
*
-* @par Description
-* Unlocks the buf_mgr context by calling
-* ih264_buf_mgr_unlock(), ithread_yield() and then ih264_buf_mgr_lock()
-* buf_mgr is unlocked before to ensure the buf_mgr can be accessed by other threads
-* If unlock is not done before calling yield then no other thread can access
-* the buf_mgr functions and update buf_mgr.
+* @par Description
+* Unlocks the buf_mgr context by calling ih264_buf_mgr_unlock(),
+* ithread_yield() and then ih264_buf_mgr_lock(). buf_mgr is unlocked before to
+* ensure the buf_mgr can be accessed by other threads. If unlock is not done
+* before calling yield then no other thread can access the buf_mgr functions
+* and update buf_mgr.
*
* @param[in] ps_buf_mgr
-* Job Queue context
+* Pointer to the buffer manager
*
* @returns IH264_FAIL if mutex lock unlock or yield fails else IH264_SUCCESS
*
@@ -169,89 +176,78 @@ IH264_ERROR_T ih264_buf_mgr_unlock(buf_mgr_t *ps_buf_mgr)
*/
IH264_ERROR_T ih264_buf_mgr_yield(buf_mgr_t *ps_buf_mgr)
{
+ IH264_ERROR_T ret;
- IH264_ERROR_T ret = IH264_SUCCESS;
-
- IH264_ERROR_T rettmp;
- rettmp = ih264_buf_mgr_unlock(ps_buf_mgr);
- RETURN_IF((rettmp != IH264_SUCCESS), rettmp);
+ ret = ih264_buf_mgr_unlock(ps_buf_mgr);
+ RETURN_IF((ret != IH264_SUCCESS), ret);
//ithread_usleep(10);
ithread_yield();
- rettmp = ih264_buf_mgr_lock(ps_buf_mgr);
- RETURN_IF((rettmp != IH264_SUCCESS), rettmp);
- return ret;
+ ret = ih264_buf_mgr_lock(ps_buf_mgr);
+ RETURN_IF((ret != IH264_SUCCESS), ret);
+ return IH264_SUCCESS;
}
-
/**
*******************************************************************************
*
-* @brief free the buf queue pointers
+* @brief frees the buffer manager context
*
-* @par Description
-* Frees the buf_mgr context
+* @par Description
+* Frees the buffer manager context
*
-* @param[in] pv_buf
-* Memoy for buf queue buffer and buf queue context
+* @param[in] ps_buf_mgr
+* Pointer to the buffer manager
*
-* @returns Pointer to buf queue context
+* @returns IH264_FAIL if mutex destory fails else IH264_SUCCESS
*
* @remarks
-* Since it will be called only once by master thread this is not thread safe.
+* Since it will be called only once by master thread this is not thread safe.
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_free(buf_mgr_t *ps_buf_mgr)
{
WORD32 ret;
- ret = ithread_mutex_destroy(ps_buf_mgr->pv_mutex);
+ ret = ithread_mutex_destroy(ps_buf_mgr->pv_mutex);
if(0 == ret)
return IH264_SUCCESS;
- else
- return IH264_FAIL;
+ return IH264_FAIL;
}
+
/**
*******************************************************************************
*
-* @brief
-* Buffer manager initialization function.
+* @brief Buffer manager context initialization.
*
* @par Description:
-* Initializes the buffer manager structure
+* Initializes the buffer manager structure
*
-* @param[in] ps_buf_mgr
+* @param[in] pv_buf
* Pointer to the buffer manager
*
-* @returns
+* @returns none
*
-* @remarks
-* None
+* @remarks none
*
*******************************************************************************
*/
-
-
-void *ih264_buf_mgr_init(void *pv_buf)
+void* ih264_buf_mgr_init(void *pv_buf)
{
WORD32 id;
- UWORD8 *pu1_buf;
- buf_mgr_t *ps_buf_mgr;
- pu1_buf = (UWORD8 *)pv_buf;
+ UWORD8 *pu1_buf = (UWORD8 *)pv_buf;
+ buf_mgr_t *ps_buf_mgr = (buf_mgr_t *)pu1_buf;
- ps_buf_mgr = (buf_mgr_t *)pu1_buf;
pu1_buf += sizeof(buf_mgr_t);
-
ps_buf_mgr->pv_mutex = pu1_buf;
- pu1_buf += ithread_get_mutex_lock_size();
+ pu1_buf += ithread_get_mutex_lock_size();
ithread_mutex_init(ps_buf_mgr->pv_mutex);
ps_buf_mgr->i4_max_buf_cnt = BUF_MGR_MAX_CNT;
ps_buf_mgr->i4_active_buf_cnt = 0;
-
for(id = 0; id < BUF_MGR_MAX_CNT; id++)
{
ps_buf_mgr->au4_status[id] = 0;
@@ -264,19 +260,17 @@ void *ih264_buf_mgr_init(void *pv_buf)
/**
*******************************************************************************
*
-* @brief
-* Buffer manager reset function.
+* @brief Buffer manager reset function.
*
* @par Description:
-* resets the buffer manager structure
+* resets the buffer manager structure
*
-* @param[in] ps_buf_mgr
+* @param[in] pv_buf
* Pointer to the buffer manager
*
-* @returns
+* @returns none
*
-* @remarks
-* None
+* @remarks none
*
*******************************************************************************
*/
@@ -298,12 +292,11 @@ void ih264_buf_mgr_reset(void *pv_buf_mgr)
/**
*******************************************************************************
*
-* @brief
-* Adds and increments the buffer and buffer count.
+* @brief Adds and increments the buffer and buffer count.
*
* @par Description:
-* Adds a buffer to the buffer manager if it is not already present and
-* increments the active buffer count
+* Adds a buffer to the buffer manager if it is not already present and
+* increments the active buffer count
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -311,10 +304,13 @@ void ih264_buf_mgr_reset(void *pv_buf_mgr)
* @param[in] pv_ptr
* Pointer to the buffer to be added
*
+* @param[in] buf_id
+* buffer id
+*
* @returns Returns 0 on success, -1 otherwise
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
@@ -322,8 +318,8 @@ IH264_ERROR_T ih264_buf_mgr_add(buf_mgr_t *ps_buf_mgr,
void *pv_ptr,
WORD32 buf_id)
{
-
IH264_ERROR_T ret = IH264_SUCCESS;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
@@ -332,17 +328,15 @@ IH264_ERROR_T ih264_buf_mgr_add(buf_mgr_t *ps_buf_mgr,
{
ret = ih264_buf_mgr_unlock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
-
return IH264_FAIL;
}
/* Check if the current ID is being used to hold some other buffer */
if((ps_buf_mgr->apv_ptr[buf_id] != NULL) &&
- (ps_buf_mgr->apv_ptr[buf_id] !=pv_ptr))
+ (ps_buf_mgr->apv_ptr[buf_id] != pv_ptr))
{
ret = ih264_buf_mgr_unlock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
-
return IH264_FAIL;
}
ps_buf_mgr->apv_ptr[buf_id] = pv_ptr;
@@ -357,12 +351,11 @@ IH264_ERROR_T ih264_buf_mgr_add(buf_mgr_t *ps_buf_mgr,
/**
*******************************************************************************
*
-* @brief
-* Gets the next free buffer.
+* @brief Gets the next free buffer.
*
* @par Description:
-* Returns the next free buffer available and sets the corresponding status
-* to DEC
+* Returns the next free buffer available and sets the corresponding status
+* to Busy
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -373,26 +366,26 @@ IH264_ERROR_T ih264_buf_mgr_add(buf_mgr_t *ps_buf_mgr,
* @returns Pointer to the free buffer
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
void* ih264_buf_mgr_get_next_free(buf_mgr_t *ps_buf_mgr, WORD32 *pi4_buf_id)
{
WORD32 id;
- void *pv_ret_ptr;
+ void *pv_ret_ptr = NULL;
IH264_ERROR_T ret = IH264_SUCCESS;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), NULL);
- pv_ret_ptr = NULL;
for(id = 0; id < ps_buf_mgr->i4_active_buf_cnt; id++)
{
/* Check if the buffer is non-null and status is zero */
if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
{
*pi4_buf_id = id;
- /* DEC is set to 1 */
+ /* mark buffer as busy before returning */
ps_buf_mgr->au4_status[id] = 1;
pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
break;
@@ -404,12 +397,10 @@ void* ih264_buf_mgr_get_next_free(buf_mgr_t *ps_buf_mgr, WORD32 *pi4_buf_id)
return pv_ret_ptr;
}
-
/**
*******************************************************************************
*
-* @brief
-* Checks the buffer manager for free buffers available.
+* @brief Checks the buffer manager for free buffers available.
*
* @par Description:
* Checks if there are any free buffers available
@@ -417,22 +408,22 @@ void* ih264_buf_mgr_get_next_free(buf_mgr_t *ps_buf_mgr, WORD32 *pi4_buf_id)
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
*
-* @returns Returns 0 if available, -1 otherwise
+* @returns Returns IH264_SUCCESS if available, IH264_FAIL otherwise
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_check_free(buf_mgr_t *ps_buf_mgr)
{
WORD32 id;
- IH264_ERROR_T ret = IH264_SUCCESS;
+ IH264_ERROR_T ret = IH264_FAIL;
IH264_ERROR_T rettmp = IH264_SUCCESS;
+
rettmp = ih264_buf_mgr_lock(ps_buf_mgr);
- RETURN_IF((rettmp != IH264_SUCCESS), ret);
+ RETURN_IF((rettmp != IH264_SUCCESS), rettmp);
- ret = IH264_FAIL;
for(id = 0; id < ps_buf_mgr->i4_active_buf_cnt; id++)
{
if((ps_buf_mgr->au4_status[id] == 0) &&
@@ -443,22 +434,19 @@ IH264_ERROR_T ih264_buf_mgr_check_free(buf_mgr_t *ps_buf_mgr)
}
}
rettmp = ih264_buf_mgr_unlock(ps_buf_mgr);
- RETURN_IF((rettmp != IH264_SUCCESS), ret);
+ RETURN_IF((rettmp != IH264_SUCCESS), rettmp);
return ret;
-
}
-
/**
*******************************************************************************
*
-* @brief
-* Resets the status bits.
+* @brief Release buffer to buffer manager
*
* @par Description:
-* resets the status bits that the mask contains (status corresponding to
-* the id)
+* Clear the status bit basing on the mask. If the buffer is no longer used by
+* the codec then release back to the pool
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -469,10 +457,10 @@ IH264_ERROR_T ih264_buf_mgr_check_free(buf_mgr_t *ps_buf_mgr)
* @param[in] mask
* Contains the bits that are to be reset
*
-* @returns 0 if success, -1 otherwise
+* @returns IH264_SUCCESS if success, IH264_FAIL otherwise
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
@@ -481,10 +469,10 @@ IH264_ERROR_T ih264_buf_mgr_release(buf_mgr_t *ps_buf_mgr,
UWORD32 mask)
{
IH264_ERROR_T ret = IH264_SUCCESS;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
-
/* If the given id is pointing to an id which is not yet added */
if(buf_id >= ps_buf_mgr->i4_active_buf_cnt)
{
@@ -495,31 +483,25 @@ IH264_ERROR_T ih264_buf_mgr_release(buf_mgr_t *ps_buf_mgr,
ps_buf_mgr->au4_status[buf_id] &= ~mask;
-
-/* If both the REF and DISP are zero, DEC is set to zero */
+ /* If both the REF and DISP are zero, DEC is set to zero */
if(ps_buf_mgr->au4_status[buf_id] == 1)
{
ps_buf_mgr->au4_status[buf_id] = 0;
}
-
ret = ih264_buf_mgr_unlock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
return ret;
}
-
/**
*******************************************************************************
*
-* @brief
-* Sets the status bit.
+* @brief Sets the status bit.
*
* @par Description:
-* sets the status bits that the mask contains (status corresponding to the
-* id)
-*
+* sets the status bits that the mask contains (status corresponding to the id)
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -527,14 +509,13 @@ IH264_ERROR_T ih264_buf_mgr_release(buf_mgr_t *ps_buf_mgr,
* @param[in] buf_id
* ID of the buffer whose status needs to be modified
*
-*
* @param[in] mask
* Contains the bits that are to be set
*
-* @returns 0 if success, -1 otherwise
+* @returns IH264_SUCCESS if success, IH264_FAIL otherwise
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
@@ -543,6 +524,7 @@ IH264_ERROR_T ih264_buf_mgr_set_status(buf_mgr_t *ps_buf_mgr,
UWORD32 mask)
{
IH264_ERROR_T ret = IH264_SUCCESS;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
@@ -553,7 +535,6 @@ IH264_ERROR_T ih264_buf_mgr_set_status(buf_mgr_t *ps_buf_mgr,
return IH264_FAIL;
}
-
if((ps_buf_mgr->au4_status[buf_id] & mask) != 0)
{
ret = ih264_buf_mgr_unlock(ps_buf_mgr);
@@ -568,12 +549,10 @@ IH264_ERROR_T ih264_buf_mgr_set_status(buf_mgr_t *ps_buf_mgr,
return ret;
}
-
/**
*******************************************************************************
*
-* @brief
-* Returns the status of the buffer.
+* @brief Returns the status of the buffer.
*
* @par Description:
* Returns the status of the buffer corresponding to the id
@@ -587,11 +566,11 @@ IH264_ERROR_T ih264_buf_mgr_set_status(buf_mgr_t *ps_buf_mgr,
* @returns Status of the buffer corresponding to the id
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
-WORD32 ih264_buf_mgr_get_status( buf_mgr_t *ps_buf_mgr, WORD32 buf_id )
+WORD32 ih264_buf_mgr_get_status(buf_mgr_t *ps_buf_mgr, WORD32 buf_id)
{
IH264_ERROR_T ret = IH264_SUCCESS;
UWORD32 status;
@@ -607,15 +586,13 @@ WORD32 ih264_buf_mgr_get_status( buf_mgr_t *ps_buf_mgr, WORD32 buf_id )
return status;
}
-
/**
*******************************************************************************
*
-* @brief
-* Gets the buffer from the buffer manager
+* @brief Gets the buffer from the buffer manager
*
* @par Description:
-* Returns the pointer to the buffer corresponding to the id
+* Returns the pointer to the buffer corresponding to the id
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -626,7 +603,7 @@ WORD32 ih264_buf_mgr_get_status( buf_mgr_t *ps_buf_mgr, WORD32 buf_id )
* @returns Pointer to the buffer required
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
@@ -634,6 +611,7 @@ void* ih264_buf_mgr_get_buf(buf_mgr_t *ps_buf_mgr, WORD32 buf_id)
{
IH264_ERROR_T ret = IH264_SUCCESS;
void *pv_buf;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), NULL);
@@ -645,13 +623,11 @@ void* ih264_buf_mgr_get_buf(buf_mgr_t *ps_buf_mgr, WORD32 buf_id)
return pv_buf;
}
-
/**
*******************************************************************************
*
-* @brief
-* Gets the buffer id from the buffer manager if the buffer is added to the
-* buffer manager
+* @brief Gets the buffer id from the buffer manager if the buffer is added to
+* the buffer manager
*
* @par Description:
* Returns the buffer id corresponding to the given buffer if it exists
@@ -665,7 +641,7 @@ void* ih264_buf_mgr_get_buf(buf_mgr_t *ps_buf_mgr, WORD32 buf_id)
* @returns Buffer id if exists, else -1
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
@@ -674,6 +650,7 @@ WORD32 ih264_buf_mgr_get_bufid(buf_mgr_t *ps_buf_mgr, void *pv_buf)
WORD32 id;
WORD32 buf_id = -1;
IH264_ERROR_T ret = IH264_SUCCESS;
+
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
@@ -691,15 +668,13 @@ WORD32 ih264_buf_mgr_get_bufid(buf_mgr_t *ps_buf_mgr, void *pv_buf)
return buf_id;
}
-
/**
*******************************************************************************
*
-* @brief
-* Gets the no.of active buffer
+* @brief Gets the no.of active buffer
*
* @par Description:
-* Return the number of active buffers in the buffer manager
+* Return the number of active buffers in the buffer manager
*
* @param[in] ps_buf_mgr
* Pointer to the buffer manager
@@ -707,19 +682,18 @@ WORD32 ih264_buf_mgr_get_bufid(buf_mgr_t *ps_buf_mgr, void *pv_buf)
* @returns number of active buffers
*
* @remarks
-* None
+* none
*
*******************************************************************************
*/
UWORD32 ih264_buf_mgr_get_num_active_buf(buf_mgr_t *ps_buf_mgr)
{
- UWORD32 u4_buf_cnt;
+ UWORD32 u4_buf_cnt = 0;
IH264_ERROR_T ret = IH264_SUCCESS;
- u4_buf_cnt = 0;
-
ret = ih264_buf_mgr_lock(ps_buf_mgr);
RETURN_IF((ret != IH264_SUCCESS), ret);
+
u4_buf_cnt = ps_buf_mgr->i4_active_buf_cnt;
ret = ih264_buf_mgr_unlock(ps_buf_mgr);