Example #1
0
/**
 ************************************************************************
 * @brief      This method deletes a thread (identified by its context). After
 *             this call the thread and its context are no more useable. This
 *             function frees all the memory related to this thread.
 * @note       Before the method is called, the state is M4OSA_kThreadOpened.
 *             Once the method is called, the state is M4OSA_kThreadClosed.
 * @param      context:(IN/OUT) Context of the thread
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_STATE: this function cannot be called now
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
 ************************************************************************
*/
M4OSA_ERR M4OSA_threadSyncClose(M4OSA_Context context)
{
   M4OSA_ThreadContext* threadContext = (M4OSA_ThreadContext*)context;
   M4OSA_ERR err_code;

   M4OSA_TRACE1_1("M4OSA_threadSyncClose\t\tM4OSA_Context 0x%x", context);

   M4OSA_DEBUG_IF2(context == M4OSA_NULL,
                   M4ERR_PARAMETER, "M4OSA_threadSyncClose");

   M4OSA_DEBUG_IF2(threadContext->coreID != M4OSA_THREAD,
                   M4ERR_BAD_CONTEXT, "M4OSA_threadSyncClose");

   M4OSA_DEBUG_IF2(threadContext->state == M4OSA_kThreadClosed,
                   M4ERR_BAD_CONTEXT, "M4OSA_threadSyncClose");

   M4OSA_mutexLock(threadContext->stateMutex, M4OSA_WAIT_FOREVER);

   if(threadContext->state != M4OSA_kThreadOpened)
   {
      M4OSA_mutexUnlock(threadContext->stateMutex);

      M4OSA_DEBUG(M4ERR_STATE, "M4OSA_threadSyncClose");

      return M4ERR_STATE;
   }

   threadContext->state = M4OSA_kThreadClosed;

   M4OSA_mutexUnlock(threadContext->stateMutex);

   err_code = M4OSA_mutexClose(threadContext->stateMutex);

   if(M4OSA_ERR_IS_ERROR(err_code))
   {
      M4OSA_DEBUG(err_code, "M4OSA_threadSyncClose: M4OSA_mutexClose");

      return err_code;
   }

   err_code = M4OSA_semaphoreClose(threadContext->semStartStop);

   if(M4OSA_ERR_IS_ERROR(err_code))
   {
      M4OSA_DEBUG(err_code, "M4OSA_threadSyncClose: M4OSA_semaphoreClose");

      return err_code;
   }

   if(threadContext->name != M4OSA_NULL)
   {
      free(threadContext->name);
   }

   free(threadContext);

   return M4NO_ERROR;
}
Example #2
0
/**
 ************************************************************************
 * @brief      This method asks the calling thread to sleep during "timeSleep"
 *             milliseconds.
 * @note       This function does not have any context.
 * @param      time:(IN) Time to sleep in milliseconds
 * @return     M4NO_ERROR: there is no error
 ************************************************************************
*/
M4OSA_ERR M4OSA_threadSleep(M4OSA_UInt32 time)
{
   struct timespec rqtp = { 0, 0 };
   struct timespec rmtp = { 0, 0 };

   M4OSA_TRACE1_1("M4OSA_threadSleep\t\tM4OSA_UInt32 %d", time);

   rqtp.tv_sec = (time_t)time/1000;
   rqtp.tv_nsec = (time%1000) * 1000000;
   nanosleep(&rqtp, &rmtp);

   return M4NO_ERROR;
}
/**
 ************************************************************************
 * @brief      This function asks the core file reader to close the file
 *             (associated to the context).
 * @note       The context of the core file reader is freed.
 * @param      pContext: (IN/OUT) Context of the core file reader
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
 * @return     M4ERR_ALLOC: there is no more memory available
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileReadClose(M4OSA_Context pContext)
{
    M4OSA_FileContext* pFileContext = (M4OSA_FileContext*)pContext;

    M4OSA_TRACE1_1("M4OSA_fileReadClose : pC = 0x%p", pContext);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext, M4ERR_PARAMETER,
                                 "M4OSA_fileReadClose: pContext is M4OSA_NULL");

    if(M4OSA_FILE_WRITER == pFileContext->coreID_write)
    {
        return M4NO_ERROR;
    }

    return M4OSA_fileCommonClose(M4OSA_FILE_READER, pContext);
}
Example #4
0
/**
 ************************************************************************
 * @brief      This method deletes a semaphore (identify by its context).
 *             After this call the semaphore and its context is no more
 *             useable. This function frees all the memory related to this
 *             semaphore.
 * @note       It is an application issue to warrant no more threads are locked
 *             on the deleted semaphore.
 * @param      context:(IN/OUT) Context of the semaphore
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one.
************************************************************************
*/
M4OSA_ERR M4OSA_semaphoreClose(M4OSA_Context context)
{
   M4OSA_SemaphoreContext* semaphoreContext = (M4OSA_SemaphoreContext*)context;

   M4OSA_TRACE1_1("M4OSA_semaphoreClose\t\tM4OSA_Context 0x%x", context);

   M4OSA_DEBUG_IF2(context == M4OSA_NULL,
                   M4ERR_PARAMETER, "M4OSA_semaphoreClose");

   M4OSA_DEBUG_IF2(semaphoreContext->coreID != M4OSA_SEMAPHORE,
                   M4ERR_BAD_CONTEXT, "M4OSA_semaphoreClose");

   sem_destroy(&semaphoreContext->semaphore);

   free(semaphoreContext);

   return M4NO_ERROR;
}
Example #5
0
/**
 ************************************************************************
 * @brief      This method increments the semaphore counter. The semaphore is
 *             identified by its context
 * @note       If the semaphore counter is upper than zero (after addition),
 *             the M4OSA_semaphoreWait call of the thread with the highest
 *             priority is unblocked and made ready to run.
 * @note       No hypotheses can be made on which thread will be unblocked
 *             between threads with the same priority.
 * @param      context:(IN/OUT) Context of the semaphore
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
************************************************************************
*/
M4OSA_ERR M4OSA_semaphorePost(M4OSA_Context context)
{
   M4OSA_SemaphoreContext* semaphoreContext = (M4OSA_SemaphoreContext*)context;

#ifndef ANDROID_DEFAULT_CODE
   M4OSA_TRACE2_1("M4OSA_semaphorePost\t\tM4OSA_Context 0x%x", context);
#else
   M4OSA_TRACE1_1("M4OSA_semaphorePost\t\tM4OSA_Context 0x%x", context);
#endif

   M4OSA_DEBUG_IF2(context == M4OSA_NULL,
                   M4ERR_PARAMETER, "M4OSA_semaphorePost");

   M4OSA_DEBUG_IF2(semaphoreContext->coreID != M4OSA_SEMAPHORE,
                   M4ERR_BAD_CONTEXT, "M4OSA_semaphorePost");

   sem_post(&semaphoreContext->semaphore);

   return M4NO_ERROR;
}
Example #6
0
/**
 ************************************************************************
 * @brief      This method stops a specified thread.
 * @note       This call is a blocking one up to the "M4OSA_ThreadDoIt"
 *             function has returned.
 *             Before the method is called, the state is M4OSA_kThreadRunning.
 *             Once the method is called, the state is M4OSA_kThreadStopping.
 *             Once the thread is stopped, the state is M4OSA_kThreadOpened.
 * @note       This method returns once the thread has been stopped. If the
 *             "threadStopped" optionID is not NULL, the thread will call it
 *             before dying.
 * @param      context:(IN/OUT) Context of the thread
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_STATE: this function cannot be called now
 * @return     M4ERR_BAD_CONTEXT: provided context is not a valid one
 ************************************************************************
*/
M4OSA_ERR M4OSA_threadSyncStop(M4OSA_Context context)
{
   M4OSA_ThreadContext* threadContext = (M4OSA_ThreadContext*)context;

   M4OSA_TRACE1_1("M4OSA_threadSyncStop\t\tM4OSA_Context 0x%x", context);

   M4OSA_DEBUG_IF2(context == M4OSA_NULL,
                   M4ERR_PARAMETER, "M4OSA_threadSyncStop");

   M4OSA_DEBUG_IF2(threadContext->coreID != M4OSA_THREAD,
                   M4ERR_BAD_CONTEXT, "M4OSA_threadSyncStop");

   M4OSA_mutexLock(threadContext->stateMutex, M4OSA_WAIT_FOREVER);

   if(threadContext->state != M4OSA_kThreadRunning)
   {
      M4OSA_mutexUnlock(threadContext->stateMutex);

      M4OSA_DEBUG(M4ERR_STATE, "M4OSA_threadSyncStop");

      return M4ERR_STATE;
   }

   threadContext->state = M4OSA_kThreadStopping;

   M4OSA_mutexUnlock(threadContext->stateMutex);

   M4OSA_semaphoreWait(threadContext->semStartStop, M4OSA_WAIT_FOREVER);

   M4OSA_mutexLock(threadContext->stateMutex, M4OSA_WAIT_FOREVER);

   threadContext->state = M4OSA_kThreadOpened;

   M4OSA_mutexUnlock(threadContext->stateMutex);

   return M4NO_ERROR;
}
Example #7
0
/**
 ************************************************************************
 * @brief      This function opens the provided URL and returns its context.
 *             If an error occured, the context is set to NULL.
 * @param      core_id: (IN) Core ID of the caller (M4OSA_FILE_READER or M4OSA_FILE_WRITER)
 * @param      context: (OUT) Context of the core file reader
 * @param      url: (IN) URL of the input file
 * @param      fileModeAccess: (IN) File mode access
 * @return     M4NO_ERROR: there is no error
 * @return     M4ERR_PARAMETER: at least one parameter is NULL
 * @return     M4ERR_ALLOC: there is no more memory available
 * @return     M4ERR_NOT_IMPLEMENTED: the URL does not match with the supported
 *             file
 * @return     M4ERR_FILE_NOT_FOUND: the file cannot be found
 * @return     M4ERR_FILE_LOCKED: the file is locked by an other
 *             application/process
 * @return     M4ERR_FILE_BAD_MODE_ACCESS: the file mode access is not correct
 ************************************************************************
*/
M4OSA_ERR M4OSA_fileCommonOpen(M4OSA_UInt16 core_id, M4OSA_Context* pContext,
                               M4OSA_Char* pUrl, M4OSA_FileModeAccess fileModeAccess)
{

    M4OSA_Int32 i            = 0;
    M4OSA_Int32 iMode        = 0;
    M4OSA_Int32 iSize        = 0;
    M4OSA_Int32 iSavePos    = 0;

    M4OSA_Char  mode[4]            = "";
    M4OSA_Char* pReadString        = (M4OSA_Char*)"r";
    M4OSA_Char* pWriteString    = (M4OSA_Char*)"w";
    M4OSA_Char* pAppendString    = (M4OSA_Char*)"a";
    M4OSA_Char* pBinaryString    = (M4OSA_Char*)"b";
    M4OSA_Char* pPlusString        = (M4OSA_Char*)"+";

    M4OSA_ERR err = M4NO_ERROR;

    FILE* pFileHandler = M4OSA_NULL;
    M4OSA_FileContext *pFileContext    = M4OSA_NULL;


#ifdef UTF_CONVERSION
    /*FB: to test the UTF16->UTF8 conversion into Video Artist*/
    /*Convert the URL from UTF16 to UTF8*/
    M4OSA_Void* tempConversionBuf;
    M4OSA_UInt32 tempConversionSize = 1000;

    tempConversionBuf = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(tempConversionSize +1, 0, "conversion buf");
    if(tempConversionBuf == M4OSA_NULL)
    {
        M4OSA_TRACE1_0("Error when allocating conversion buffer\n");
        return M4ERR_PARAMETER;
    }
    M4OSA_ToUTF8_OSAL(pUrl, tempConversionBuf, &tempConversionSize);
    ((M4OSA_Char*)tempConversionBuf)[tempConversionSize ] = '\0';

    printf("file open %s\n", tempConversionBuf);
#endif /*UTF CONVERSION*/

    M4OSA_TRACE3_4("M4OSA_fileCommonOpen\t\tM4OSA_UInt16 %d\tM4OSA_Context* 0x%x\t"
        "M4OSA_Char* %s\tfileModeAccess %d", core_id, pContext, pUrl, fileModeAccess);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pContext is M4OSA_NULL");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pUrl,        M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: pUrl  is M4OSA_NULL");
    M4OSA_DEBUG_IF2(0 == fileModeAccess,    M4ERR_PARAMETER,    "M4OSA_fileCommonOpen: fileModeAccess is 0");

    /* Read mode not set for the reader */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");

    /* Read mode not set for the reader */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && !(fileModeAccess & M4OSA_kFileRead),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileRead");

    /* M4OSAfileReadOpen cannot be used with Write file mode access */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileWrite),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");

    /* Append and Create flags cannot be used with Read */
    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileAppend),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileAppend");

    M4OSA_DEBUG_IF1((M4OSA_FILE_READER == core_id) && (fileModeAccess & M4OSA_kFileCreate),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileCreate");

    /* Write mode not set for the writer */
    M4OSA_DEBUG_IF1((M4OSA_FILE_WRITER == core_id) && !(fileModeAccess & M4OSA_kFileWrite),
        M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: M4OSA_kFileWrite");

    /* Create flag necessary for opening file */
    if ((fileModeAccess & M4OSA_kFileRead) &&
        (fileModeAccess & M4OSA_kFileWrite)&&(fileModeAccess & M4OSA_kFileCreate))
    {
        strncat((char *)mode, (const char *)pWriteString, (size_t)1);
        strncat((char *)mode, (const char *)pPlusString, (size_t)1);
    }
    else
    {
        if(fileModeAccess & M4OSA_kFileAppend)
        {
            strncat((char *)mode, (const char *)pAppendString, (size_t)1);
        }
        else if(fileModeAccess & M4OSA_kFileRead)
        {
            strncat((char *)mode, (const char *)pReadString, (size_t)1);
        }
        else if(fileModeAccess & M4OSA_kFileWrite)
        {
            strncat((char *)mode, (const char *)pWriteString, (size_t)1);
        }

        if((fileModeAccess & M4OSA_kFileRead)&&(fileModeAccess & M4OSA_kFileWrite))
        {
            strncat((char *)mode,(const char *)pPlusString, (size_t)1);
        }
    }

    if(!(fileModeAccess & M4OSA_kFileIsTextMode))
    {
        strncat((char *)mode, (const char *)pBinaryString,(size_t)1);
    }

    /*Open the file*/

#ifdef UTF_CONVERSION
    /*Open the converted path*/
    pFileHandler = fopen((const char *)tempConversionBuf, (const char *)mode);
    /*Free the temporary decoded buffer*/
    free(tempConversionBuf);
#else /* UTF_CONVERSION */
    pFileHandler = fopen((const char *)pUrl, (const char *)mode);
#endif /* UTF_CONVERSION */

    if (M4OSA_NULL == pFileHandler)
    {
        switch(errno)
        {
        case ENOENT:
            {
                M4OSA_DEBUG(M4ERR_FILE_NOT_FOUND, "M4OSA_fileCommonOpen: No such file or directory");
                M4OSA_TRACE1_1("File not found: %s", pUrl);
                return M4ERR_FILE_NOT_FOUND;
            }
        case EACCES:
            {
                M4OSA_DEBUG(M4ERR_FILE_LOCKED, "M4OSA_fileCommonOpen: Permission denied");
                return M4ERR_FILE_LOCKED;
            }
         case EINVAL:
         {
            M4OSA_DEBUG(M4ERR_FILE_BAD_MODE_ACCESS, "M4OSA_fileCommonOpen: Invalid Argument");
            return M4ERR_FILE_BAD_MODE_ACCESS;
         }
        case EMFILE:
         case ENOSPC:
        case ENOMEM:
            {
                M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen: Too many open files");
                return M4ERR_ALLOC;
            }
        default:
            {
                M4OSA_DEBUG(M4ERR_NOT_IMPLEMENTED, "M4OSA_fileCommonOpen");
                return M4ERR_NOT_IMPLEMENTED;
            }
        }
    }

    /* Allocate the file context */
    pFileContext = (M4OSA_FileContext*) M4OSA_32bitAlignedMalloc(sizeof(M4OSA_FileContext),
                    core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: file context");
    if (M4OSA_NULL == pFileContext)
    {
        fclose(pFileHandler);
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
        return M4ERR_ALLOC;
    }

    pFileContext->file_desc        = pFileHandler;
#ifndef ANDROID_DEFAULT_CODE
    M4OSA_TRACE1_2("open file %s:%x", (char*)pUrl, pFileHandler);
#endif
    pFileContext->access_mode    = fileModeAccess;
    pFileContext->current_seek    = SeekNone;
    pFileContext->b_is_end_of_file    = M4OSA_FALSE;

    /**
     * Note: Never use this expression "i = (value1 == value2) ? x: y;"
     * because that doens't compile on other platforms (ADS for example)
     * Use: if(value1 == value2)
     *        { i= x; ..etc
     */
    pFileContext->coreID_write = 0;
    pFileContext->coreID_read = 0;
    pFileContext->m_DescrModeAccess = M4OSA_kDescNoneAccess;

    if (M4OSA_FILE_READER == core_id)
    {
        pFileContext->coreID_read = core_id;
        pFileContext->m_DescrModeAccess = M4OSA_kDescReadAccess;
    }
    else if (M4OSA_FILE_WRITER == core_id)
    {
        pFileContext->coreID_write = core_id;
        pFileContext->m_DescrModeAccess = M4OSA_kDescWriteAccess;
    }

    pFileContext->read_position = 0;
    pFileContext->write_position = 0;

    /* Allocate the memory to store the URL string */
    pFileContext->url_name = (M4OSA_Char*) M4OSA_32bitAlignedMalloc(strlen((const char *)pUrl)+1,
                        core_id, (M4OSA_Char*)"M4OSA_fileCommonOpen: URL name");
    if (M4OSA_NULL == pFileContext->url_name)
    {
        fclose(pFileHandler);
        free(pFileContext);
        M4OSA_DEBUG(M4ERR_ALLOC, "M4OSA_fileCommonOpen");
        return M4ERR_ALLOC;
    }
    M4OSA_chrNCopy(pFileContext->url_name, pUrl, strlen((const char *)pUrl)+1);

    /* Get the file name */
    err = M4OSA_fileCommonGetFilename(pUrl, &pFileContext->file_name);
    if(M4NO_ERROR != err)
    {
        fclose(pFileHandler);
        free(pFileContext->url_name);
        free(pFileContext);
        M4OSA_DEBUG(err, "M4OSA_fileCommonOpen");
        return err;
    }

#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
    M4OSA_semaphoreOpen(&(pFileContext->semaphore_context), 1); /* Allocate the semaphore */
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */



#ifdef USE_STAGEFRIGHT_CODECS
    // Workaround for file system bug on Stingray/Honeycomb where a file re-created will keep
    // the original file's size filled with 0s. Do not seek to the end to avoid ill effects
    if(fileModeAccess & M4OSA_kFileAppend) {
        /* Get the file size */
        iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
        fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
        iSize = ftell(pFileHandler);            /*    3- Check the file size        */
        fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
    } else {
        iSize = 0;
    }
#else /* USE_STAGEFRIGHT_CODECS */
    /* Get the file size */
    iSavePos = ftell(pFileHandler);            /*    1- Check the first position */
    fseek(pFileHandler, 0, SEEK_END);        /*    2- Go to the end of the file*/
    iSize = ftell(pFileHandler);            /*    3- Check the file size        */
    fseek(pFileHandler, iSavePos, SEEK_SET);/*    4- go to the first position */
#endif /* USE_STAGEFRIGHT_CODECS */



    /* Warning possible overflow if the file is higher than 2GBytes */
    pFileContext->file_size = iSize;

    *pContext = pFileContext;

    return M4NO_ERROR;
}
M4OSA_ERR M4OSA_fileReadSeek(M4OSA_Context pContext, M4OSA_FileSeekAccessMode seekMode,
                             M4OSA_FilePosition* pPosition)
{
    M4OSA_FileContext* pFileContext = (M4OSA_FileContext*)pContext;
    M4OSA_ERR err;

    M4OSA_TRACE2_2("M4OSA_fileReadSeek : mode = %d  pos = %lu", seekMode,
                                  (pPosition != M4OSA_NULL) ? (*pPosition) : 0);

    M4OSA_DEBUG_IF2(M4OSA_NULL == pContext, M4ERR_PARAMETER,
                                  "M4OSA_fileReadSeek: pContext is M4OSA_NULL");
    M4OSA_DEBUG_IF2(0 == seekMode, M4ERR_PARAMETER,
                                           "M4OSA_fileReadSeek: seekMode is 0");
    M4OSA_DEBUG_IF2(M4OSA_NULL == pPosition, M4ERR_PARAMETER,
                                 "M4OSA_fileReadSeek: pPosition is M4OSA_NULL");
#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
    M4OSA_DEBUG_IF2(M4OSA_NULL == pFileContext->semaphore_context,
      M4ERR_BAD_CONTEXT, "M4OSA_fileReadSeek: semaphore_context is M4OSA_NULL");
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */

    if (M4OSA_kDescRWAccess == pFileContext->m_DescrModeAccess)
    {
         M4OSA_UInt32    SeekModeOption;
         /* Go to the desired position */
        if (M4OSA_kFileSeekBeginning == seekMode)
        {
            SeekModeOption = SEEK_SET;
        }
        else if (M4OSA_kFileSeekEnd == seekMode)
        {
            SeekModeOption = SEEK_END;
        }
        else if (M4OSA_kFileSeekCurrent == seekMode)
        {
            SeekModeOption = SEEK_CUR;
        }
        else
        {
            M4OSA_TRACE1_0("M4OSA_fileReadSeek: END WITH ERROR !!! (CONVERION ERROR FOR THE SEEK MODE)");
            return M4ERR_PARAMETER;
        }

        /**
         * Go to the desired position */
        err = fseek(pFileContext->file_desc, *pPosition, SeekModeOption);
        if(err != 0)
        {
            /* converts the error to PSW format*/
            err=((M4OSA_UInt32)(M4_ERR)<<30)+(((M4OSA_FILE_WRITER)&0x003FFF)<<16)+(M4OSA_Int16)(err);
            M4OSA_TRACE1_1("M4OSA_FileReadSeek error:%x",err);
        }
        else
        {
            return M4NO_ERROR;
        }

        /* Return without error */
        return err;
    }


#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
    M4OSA_semaphoreWait(pFileContext->semaphore_context, M4OSA_WAIT_FOREVER);
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */

    if(pFileContext->current_seek != SeekRead)
    {

        /* fseek to the last read position */
        err = M4OSA_fileCommonSeek(pContext, M4OSA_kFileSeekBeginning,
                                                &(pFileContext->read_position));
        if(M4OSA_ERR_IS_ERROR(err))
        {
            M4OSA_DEBUG(err, "M4OSA_fileReadData: M4OSA_fileCommonSeek");

#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
            M4OSA_semaphorePost(pFileContext->semaphore_context);
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */

            return err;
        }

        pFileContext->current_seek = SeekRead;
    }

    err = M4OSA_fileCommonSeek(pContext, seekMode, pPosition);
    if(M4OSA_ERR_IS_ERROR(err))
    {
        M4OSA_DEBUG(err, "M4OSA_fileReadData: M4OSA_fileCommonSeek");
    }
    else
    {
        pFileContext->read_position = *pPosition;
    }

#ifdef M4OSA_FILE_BLOCK_WITH_SEMAPHORE
    M4OSA_semaphorePost(pFileContext->semaphore_context);
#endif /* M4OSA_FILE_BLOCK_WITH_SEMAPHORE */

    return err;
}
/**
 ******************************************************************************
 * M4OSA_ERR M4AIR_configure_NV12(M4OSA_Context pContext, M4AIR_Params* pParams)
 * @brief   This function will configure the AIR.
 * @note    It will set the input and output coordinates and sizes,
 *          and indicates if we will proceed in stripe or not.
 *          In case a M4AIR_get in stripe mode was on going, it will cancel this previous
 *          processing and reset the get process.
 * @param    pContext:                (IN) Context identifying the instance
 * @param    pParams->m_bOutputStripe:(IN) Stripe mode.
 * @param    pParams->m_inputCoord:    (IN) X,Y coordinates of the first valid pixel in input.
 * @param    pParams->m_inputSize:    (IN) input ROI size.
 * @param    pParams->m_outputSize:    (IN) output size.
 * @return    M4NO_ERROR: there is no error
 * @return    M4ERR_ALLOC: No more memory space to add a new effect.
 * @return    M4ERR_PARAMETER: pContext is M4OSA_NULL (debug only).
 * @return    M4ERR_AIR_FORMAT_NOT_SUPPORTED: the requested input format is not supported.
 ******************************************************************************
 */
M4OSA_ERR M4AIR_configure_NV12(M4OSA_Context pContext, M4AIR_Params* pParams)
{
    M4AIR_InternalContext* pC = (M4AIR_InternalContext*)pContext ;
    M4OSA_UInt32    i,u32_width_in, u32_width_out, u32_height_in, u32_height_out;
    M4OSA_UInt32    nb_planes;

    M4OSA_TRACE1_0("M4AIR_configure_NV12 start");
    M4ERR_CHECK_NULL_RETURN_VALUE(M4ERR_PARAMETER, pContext);

    if(M4AIR_kNV12P == pC->m_inputFormat)
    {
        nb_planes = 2;
    }
    else
    {
        nb_planes = 3;
    }

    /**< Check state */
    if((M4AIR_kCreated != pC->m_state)&&(M4AIR_kConfigured != pC->m_state))
    {
        return M4ERR_STATE;
    }

    /** Save parameters */
    pC->m_params = *pParams;

    /* Check for the input&output width and height are even */
    if(((pC->m_params.m_inputSize.m_height)&0x1) ||
        ((pC->m_params.m_outputSize.m_height)&0x1))
    {
        return M4ERR_AIR_ILLEGAL_FRAME_SIZE;
    }

    if(((pC->m_params.m_inputSize.m_width)&0x1)||
        ((pC->m_params.m_outputSize.m_width)&0x1))
    {
        return M4ERR_AIR_ILLEGAL_FRAME_SIZE;
    }
    if(((pC->m_params.m_inputSize.m_width) == (pC->m_params.m_outputSize.m_width))
        &&((pC->m_params.m_inputSize.m_height) == (pC->m_params.m_outputSize.m_height)))
    {
        /**< No resize in this case, we will just copy input in output */
        pC->m_bOnlyCopy = M4OSA_TRUE;
    }
    else
    {
        pC->m_bOnlyCopy = M4OSA_FALSE;

        /**< Initialize internal variables used for resize filter */
        for(i=0;i<nb_planes;i++)
        {
            M4OSA_TRACE1_1("Plane %d", i);
            u32_width_in = pC->m_params.m_inputSize.m_width;
            u32_height_in = ((i==0)||(i==2))?pC->m_params.m_inputSize.m_height:\
                (pC->m_params.m_inputSize.m_height+1)>>1;
            u32_width_out = pC->m_params.m_outputSize.m_width;
            u32_height_out = ((i==0)||(i==2))?pC->m_params.m_outputSize.m_height:\
                (pC->m_params.m_outputSize.m_height+1)>>1;

            M4OSA_TRACE1_4("u32_width_in =%d, u32_height_in = %d, u32_width_out = %d, u32_height_out = %d",\
                u32_width_in, u32_height_in, u32_width_out, u32_height_out);

            /* Compute horizontal ratio between src and destination width.*/
            if (u32_width_out >= u32_width_in)
            {
                if (i == 1)
                {
                    pC->u32_x_inc[i] = ((u32_width_in-2) * 0x10000) / (u32_width_out-2);
                }
                else
                {
                    pC->u32_x_inc[i] = ((u32_width_in-1) * 0x10000) / (u32_width_out-1);
                }
            }
            else
            {
                pC->u32_x_inc[i] = (u32_width_in * 0x10000) / (u32_width_out);
            }

            /* Compute vertical ratio between src and destination height.*/
            if (u32_height_out >= u32_height_in)
            {
                pC->u32_y_inc[i] = ((u32_height_in - 1) * 0x10000) / (u32_height_out-1);
            }
            else
            {
                pC->u32_y_inc[i] = (u32_height_in * 0x10000) / (u32_height_out);
            }

            /*
                    Calculate initial accumulator value : u32_y_accum_start.
                    u32_y_accum_start is coded on 15 bits, and represents a value between 0 and 0.5
                     */
            if (pC->u32_y_inc[i] >= 0x10000)
            {
            /*
                    Keep the fractionnal part, assimung that integer  part is coded
                    on the 16 high bits and the fractionnal on the 15 low bits
                     */
                pC->u32_y_accum_start[i] = pC->u32_y_inc[i] & 0xffff;

                if (!pC->u32_y_accum_start[i])
                {
                    pC->u32_y_accum_start[i] = 0x10000;
                }

                pC->u32_y_accum_start[i] >>= 1;
            }
            else
            {
                pC->u32_y_accum_start[i] = 0;
            }
            /**< Take into account that Y coordinate can be odd
                in this case we have to put a 0.5 offset
                for U and V plane as there a 2 times sub-sampled vs Y*/
            if((pC->m_params.m_inputCoord.m_y&0x1)&&(i==1))
            {
                pC->u32_y_accum_start[i] += 0x8000;
            }

            /*
                    Calculate initial accumulator value : u32_x_accum_start.
                    u32_x_accum_start is coded on 15 bits, and represents a value between
                    0 and 0.5
                     */

            if (pC->u32_x_inc[i] >= 0x10000)
            {
                pC->u32_x_accum_start[i] = pC->u32_x_inc[i] & 0xffff;

                if (!pC->u32_x_accum_start[i])
                {
                    pC->u32_x_accum_start[i] = 0x10000;
                }

                pC->u32_x_accum_start[i] >>= 1;
            }