M4MCS_AudioEffects.c revision b5c7784c96a606890eb8a8b560153ef4a5d1a0d9
1/*
2 * Copyright (C) 2011 The Android Open Source Project
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 *************************************************************************
18 * @file   M4MCS_API.c
19 * @brief  MCS implementation (Video Compressor Service)
20 * @note   This file implements the API and the processing of the MCS
21 *************************************************************************
22 **/
23
24/****************/
25/*** Includes ***/
26/****************/
27
28/**
29 * OSAL headers */
30#include "M4OSA_Memory.h"   /**< OSAL memory management */
31#include "M4OSA_Debug.h"    /**< OSAL debug management */
32
33/* Our headers */
34#include "M4MCS_API.h"
35#include "M4MCS_ErrorCodes.h"
36#include "M4MCS_InternalTypes.h"
37#include "M4MCS_InternalConfig.h"
38#include "M4MCS_InternalFunctions.h"
39
40/* Common headers (for aac) */
41#include "M4_Common.h"
42
43#ifdef M4VSS_ENABLE_EXTERNAL_DECODERS
44#include "M4VD_EXTERNAL_Interface.h"
45#endif /* M4VSS_ENABLE_EXTERNAL_DECODERS */
46
47
48
49/**
50 ******************************************************************************
51 * M4OSA_ERR M4MCS_intCheckAudioEffects(M4MCS_InternalContext* pContext)
52 * @brief    Check if an effect has to be applied currently
53 * @note    It is called by the stepEncoding function
54 * @param    pContext    (IN) MCS internal context
55 * @return    M4NO_ERROR:    No error
56 ******************************************************************************
57 */
58M4OSA_ERR M4MCS_intCheckAudioEffects(M4MCS_InternalContext* pC)
59{
60    M4OSA_Int8 *pActiveEffectNumber = &(pC->pActiveEffectNumber);
61
62    *pActiveEffectNumber = -1;
63
64    if(pC->ReaderAudioAU.m_CTS > pC->uiBeginCutTime
65    && pC->ReaderAudioAU.m_CTS < pC->uiEndCutTime)
66    {
67        M4OSA_UInt32 outputRelatedTime = 0;
68        M4OSA_UInt8 uiEffectIndex = 0;
69        outputRelatedTime =
70        (M4OSA_UInt32)(pC->ReaderAudioAU.m_CTS  - pC->uiBeginCutTime + 0.5);
71
72        for(uiEffectIndex=0; uiEffectIndex<pC->nbEffects; uiEffectIndex++)
73        {
74            if ((outputRelatedTime >=
75                (M4OSA_UInt32)(pC->pEffects[uiEffectIndex].uiStartTime)) &&
76                (outputRelatedTime <
77                (M4OSA_UInt32)(pC->pEffects[uiEffectIndex].uiStartTime +\
78                pC->pEffects[uiEffectIndex].uiDuration)))
79            {
80                *pActiveEffectNumber = uiEffectIndex;
81                uiEffectIndex = pC->nbEffects;
82            }
83        }
84    }
85
86    return M4NO_ERROR;
87}
88
89
90/**
91 ******************************************************************************
92 * M4OSA_ERR M4MCS_editAudioEffectFct_FadeIn()
93 * @brief    Apply audio effect FadeIn to pPCMdata
94 * @param   pC            (IN/OUT) Internal edit context
95 * @param    pPCMdata    (IN/OUT) Input and Output PCM audio data
96 * @param    uiPCMsize    (IN)     Size of pPCMdata
97 * @param    pProgress    (IN)     Effect progress
98 * @return    M4NO_ERROR:             No error
99 ******************************************************************************
100 */
101M4OSA_ERR M4MCS_editAudioEffectFct_FadeIn(  M4OSA_Void *pFunctionContext,
102                                            M4OSA_Int16 *pPCMdata,
103                                            M4OSA_UInt32 uiPCMsize,
104                                            M4MCS_ExternalProgress *pProgress)
105{
106    /* we will cast each Int16 sample into this Int32 variable */
107    M4OSA_Int32 i32sample;
108
109    /**
110     * Sanity check */
111    if(pProgress->uiProgress > 1000)
112    {
113        pProgress->uiProgress = 1000;
114    }
115
116    /**
117     * From buffer size (bytes) to number of sample (int16): divide by two */
118    uiPCMsize >>= 1;
119
120    /**
121     * Loop on samples */
122    while (uiPCMsize-->0) /**< decrementing to optimize */
123    {
124        i32sample = *pPCMdata;
125        i32sample *= pProgress->uiProgress;
126        i32sample /= 1000;
127        *pPCMdata++ = (M4OSA_Int16)i32sample;
128    }
129
130    /**
131     *    Return */
132    M4OSA_TRACE3_0("M4MCS_editAudioEffectFct_FadeIn: returning M4NO_ERROR");
133    return M4NO_ERROR;
134}
135
136
137/**
138 ******************************************************************************
139 * M4OSA_ERR M4MCS_editAudioEffectFct_FadeOut()
140 * @brief    Apply audio effect FadeIn to pPCMdata
141 * @param    pC            (IN/OUT) Internal edit context
142 * @param    pPCMdata    (IN/OUT) Input and Output PCM audio data
143 * @param    uiPCMsize    (IN)     Size of pPCMdata
144 * @param    pProgress    (IN)     Effect progress
145 * @return   M4NO_ERROR:             No error
146 ******************************************************************************
147 */
148M4OSA_ERR M4MCS_editAudioEffectFct_FadeOut( M4OSA_Void *pFunctionContext,
149                                            M4OSA_Int16 *pPCMdata,
150                                            M4OSA_UInt32 uiPCMsize,
151                                            M4MCS_ExternalProgress *pProgress)
152{
153    /* we will cast each Int16 sample into this Int32 variable */
154    M4OSA_Int32 i32sample;
155
156    /**
157     * Sanity check */
158    if(pProgress->uiProgress > 1000)
159    {
160        pProgress->uiProgress = 1000;
161    }
162    pProgress->uiProgress = 1000 - pProgress->uiProgress;
163
164    /**
165     * From buffer size (bytes) to number of sample (int16): divide by two */
166    uiPCMsize >>= 1;
167
168    /**
169     * Loop on samples */
170    while (uiPCMsize-->0) /**< decrementing to optimize */
171    {
172        i32sample = *pPCMdata;
173        i32sample *= pProgress->uiProgress;
174        i32sample /= 1000;
175        *pPCMdata++ = (M4OSA_Int16)i32sample;
176    }
177
178    /**
179     *    Return */
180    M4OSA_TRACE3_0("M4MCS_editAudioEffectFct_FadeOut: returning M4NO_ERROR");
181    return M4NO_ERROR;
182}
183
184