omx_volume_component.c

Go to the documentation of this file.
00001 
00030 #include <omxcore.h>
00031 #include <omx_base_audio_port.h>
00032 #include <omx_volume_component.h>
00033 #include<OMX_Audio.h>
00034 
00035 /* gain value */
00036 #define GAIN_VALUE 100.0f
00037 
00038 /* Max allowable volume component instance */
00039 #define MAX_COMPONENT_VOLUME 1
00040 
00042 static OMX_U32 noVolumeCompInstance = 0;
00043 
00044 
00045 OMX_ERRORTYPE omx_volume_component_Constructor(OMX_COMPONENTTYPE *openmaxStandComp, OMX_STRING cComponentName) {
00046   OMX_ERRORTYPE err = OMX_ErrorNone;  
00047   omx_volume_component_PrivateType* omx_volume_component_Private;
00048   OMX_U32 i;
00049 
00050   if (!openmaxStandComp->pComponentPrivate) {
00051     DEBUG(DEB_LEV_FUNCTION_NAME, "In %s, allocating component\n",__func__);
00052     openmaxStandComp->pComponentPrivate = calloc(1, sizeof(omx_volume_component_PrivateType));
00053     if(openmaxStandComp->pComponentPrivate == NULL) {
00054       return OMX_ErrorInsufficientResources;
00055     }
00056   } else {
00057     DEBUG(DEB_LEV_FUNCTION_NAME, "In %s, Error Component %x Already Allocated\n", __func__, (int)openmaxStandComp->pComponentPrivate);
00058   }
00059 
00060   omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00061   omx_volume_component_Private->ports = NULL;
00062   
00064   err = omx_base_filter_Constructor(openmaxStandComp, cComponentName);
00065 
00066   omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nStartPortNumber = 0;
00067   omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nPorts = 2;
00068 
00070   if (omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nPorts && !omx_volume_component_Private->ports) {
00071     omx_volume_component_Private->ports = calloc(omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nPorts, sizeof(omx_base_PortType *));
00072     if (!omx_volume_component_Private->ports) {
00073       return OMX_ErrorInsufficientResources;
00074     }
00075     for (i=0; i < omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nPorts; i++) {
00076       omx_volume_component_Private->ports[i] = calloc(1, sizeof(omx_base_audio_PortType));
00077       if (!omx_volume_component_Private->ports[i]) {
00078         return OMX_ErrorInsufficientResources;
00079       }
00080     }
00081   }
00082 
00083   base_audio_port_Constructor(openmaxStandComp, &omx_volume_component_Private->ports[0], 0, OMX_TRUE);
00084   base_audio_port_Constructor(openmaxStandComp, &omx_volume_component_Private->ports[1], 1, OMX_FALSE);
00085   
00087   omx_volume_component_Private->ports[OMX_BASE_FILTER_INPUTPORT_INDEX]->sPortParam.nBufferSize = DEFAULT_OUT_BUFFER_SIZE;
00088   omx_volume_component_Private->ports[OMX_BASE_FILTER_OUTPUTPORT_INDEX]->sPortParam.nBufferSize = DEFAULT_OUT_BUFFER_SIZE;
00089 
00090   omx_volume_component_Private->gain = GAIN_VALUE; //100.0f; // default gain
00091   omx_volume_component_Private->destructor = omx_volume_component_Destructor;
00092   openmaxStandComp->SetParameter = omx_volume_component_SetParameter;
00093   openmaxStandComp->GetParameter = omx_volume_component_GetParameter;
00094   openmaxStandComp->GetConfig = omx_volume_component_GetConfig;
00095   openmaxStandComp->SetConfig = omx_volume_component_SetConfig;
00096   omx_volume_component_Private->BufferMgmtCallback = omx_volume_component_BufferMgmtCallback;
00097 
00098   noVolumeCompInstance++;
00099   if(noVolumeCompInstance > MAX_COMPONENT_VOLUME) {
00100     return OMX_ErrorInsufficientResources;
00101   }
00102 
00103   return err;
00104 }
00105 
00106 
00109 OMX_ERRORTYPE omx_volume_component_Destructor(OMX_COMPONENTTYPE *openmaxStandComp) {
00110 
00111   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00112   OMX_U32 i;
00113 
00114   /* frees port/s */
00115   if (omx_volume_component_Private->ports) {
00116     for (i=0; i < omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio].nPorts; i++) {
00117       if(omx_volume_component_Private->ports[i])
00118         omx_volume_component_Private->ports[i]->PortDestructor(omx_volume_component_Private->ports[i]);
00119     }
00120     free(omx_volume_component_Private->ports);
00121     omx_volume_component_Private->ports=NULL;
00122   }
00123 
00124   DEBUG(DEB_LEV_FUNCTION_NAME, "Destructor of audiodecoder component is called\n");
00125   omx_base_filter_Destructor(openmaxStandComp);
00126   noVolumeCompInstance--;
00127 
00128   return OMX_ErrorNone;
00129 }
00130 
00133 void omx_volume_component_BufferMgmtCallback(OMX_COMPONENTTYPE *openmaxStandComp, OMX_BUFFERHEADERTYPE* pInputBuffer, OMX_BUFFERHEADERTYPE* pOutputBuffer) {
00134   int i;
00135   int sampleCount = pInputBuffer->nFilledLen / 2; // signed 16 bit samples assumed
00136   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00137 
00138   if(omx_volume_component_Private->gain != GAIN_VALUE) {
00139     for (i = 0; i < sampleCount; i++) {
00140       ((OMX_S16*) pOutputBuffer->pBuffer)[i] = (OMX_S16)
00141               (((OMX_S16*) pInputBuffer->pBuffer)[i] * (omx_volume_component_Private->gain / 100.0f));
00142     }
00143   } else {
00144     memcpy(pOutputBuffer->pBuffer,pInputBuffer->pBuffer,pInputBuffer->nFilledLen);
00145   }
00146   pOutputBuffer->nFilledLen = pInputBuffer->nFilledLen;
00147   pInputBuffer->nFilledLen=0;
00148 }
00149 
00151 OMX_ERRORTYPE omx_volume_component_SetConfig(
00152   OMX_IN  OMX_HANDLETYPE hComponent,
00153   OMX_IN  OMX_INDEXTYPE nIndex,
00154   OMX_IN  OMX_PTR pComponentConfigStructure) {
00155 
00156   OMX_AUDIO_CONFIG_VOLUMETYPE* pVolume;
00157   OMX_COMPONENTTYPE *openmaxStandComp = (OMX_COMPONENTTYPE *)hComponent;
00158   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00159   OMX_ERRORTYPE err = OMX_ErrorNone;
00160 
00161   switch (nIndex) {
00162     case OMX_IndexConfigAudioVolume : 
00163       pVolume = (OMX_AUDIO_CONFIG_VOLUMETYPE*) pComponentConfigStructure;
00164       if(pVolume->sVolume.nValue > 100) {
00165         err = OMX_ErrorBadParameter;
00166         break;
00167       }
00168       omx_volume_component_Private->gain = pVolume->sVolume.nValue;
00169       err = OMX_ErrorNone;
00170       break;
00171     default: // delegate to superclass
00172       err = omx_base_component_SetConfig(hComponent, nIndex, pComponentConfigStructure);
00173   }
00174 
00175   return err;
00176 }
00177 
00178 OMX_ERRORTYPE omx_volume_component_GetConfig(
00179   OMX_IN  OMX_HANDLETYPE hComponent,
00180   OMX_IN  OMX_INDEXTYPE nIndex,
00181   OMX_INOUT OMX_PTR pComponentConfigStructure) {
00182   OMX_AUDIO_CONFIG_VOLUMETYPE* pVolume;
00183   OMX_COMPONENTTYPE *openmaxStandComp = (OMX_COMPONENTTYPE *)hComponent;
00184   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00185   OMX_ERRORTYPE err = OMX_ErrorNone;
00186 
00187   switch (nIndex) {
00188     case OMX_IndexConfigAudioVolume : 
00189       pVolume = (OMX_AUDIO_CONFIG_VOLUMETYPE*) pComponentConfigStructure;
00190       setHeader(pVolume,sizeof(OMX_AUDIO_CONFIG_VOLUMETYPE));
00191       pVolume->sVolume.nValue = omx_volume_component_Private->gain;
00192       pVolume->sVolume.nMin = 0;
00193       pVolume->sVolume.nMax = 100;
00194       pVolume->bLinear = OMX_TRUE;
00195       err = OMX_ErrorNone;
00196       break;
00197     default :
00198       err = omx_base_component_GetConfig(hComponent, nIndex, pComponentConfigStructure);
00199   }
00200   return err;
00201 }
00202 
00203 OMX_ERRORTYPE omx_volume_component_SetParameter(
00204   OMX_IN  OMX_HANDLETYPE hComponent,
00205   OMX_IN  OMX_INDEXTYPE nParamIndex,
00206   OMX_IN  OMX_PTR ComponentParameterStructure) {
00207 
00208   OMX_ERRORTYPE err = OMX_ErrorNone;
00209   OMX_AUDIO_PARAM_PORTFORMATTYPE *pAudioPortFormat;
00210   OMX_U32 portIndex;
00211   omx_base_audio_PortType *port;
00212 
00213   /* Check which structure we are being fed and make control its header */
00214   OMX_COMPONENTTYPE *openmaxStandComp = (OMX_COMPONENTTYPE *)hComponent;
00215   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00216   if (ComponentParameterStructure == NULL) {
00217     return OMX_ErrorBadParameter;
00218   }
00219 
00220   DEBUG(DEB_LEV_SIMPLE_SEQ, "   Setting parameter %i\n", nParamIndex);
00221   switch(nParamIndex) {
00222     case OMX_IndexParamAudioPortFormat:
00223       pAudioPortFormat = (OMX_AUDIO_PARAM_PORTFORMATTYPE*)ComponentParameterStructure;
00224       portIndex = pAudioPortFormat->nPortIndex;
00225       err = omx_base_component_ParameterSanityCheck(hComponent, portIndex, pAudioPortFormat, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
00226       if(err!=OMX_ErrorNone) { 
00227         DEBUG(DEB_LEV_ERR, "In %s Parameter Check Error=%x\n",__func__,err); 
00228         break;
00229       }
00230       if (portIndex <= 1) {
00231         port= (omx_base_audio_PortType *)omx_volume_component_Private->ports[portIndex];
00232         memcpy(&port->sAudioParam, pAudioPortFormat, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
00233       } else {
00234         err = OMX_ErrorBadPortIndex;
00235       }
00236       break;  
00237     default:
00238       err = omx_base_component_SetParameter(hComponent, nParamIndex, ComponentParameterStructure);
00239   }
00240   return err;
00241 }
00242 
00243 OMX_ERRORTYPE omx_volume_component_GetParameter(
00244   OMX_IN  OMX_HANDLETYPE hComponent,
00245   OMX_IN  OMX_INDEXTYPE nParamIndex,
00246   OMX_INOUT OMX_PTR ComponentParameterStructure) {
00247 
00248   OMX_AUDIO_PARAM_PORTFORMATTYPE *pAudioPortFormat;  
00249   OMX_AUDIO_PARAM_PCMMODETYPE *pAudioPcmMode;
00250   OMX_ERRORTYPE err = OMX_ErrorNone;
00251   omx_base_audio_PortType *port;
00252   OMX_COMPONENTTYPE *openmaxStandComp = (OMX_COMPONENTTYPE *)hComponent;
00253   omx_volume_component_PrivateType* omx_volume_component_Private = openmaxStandComp->pComponentPrivate;
00254   if (ComponentParameterStructure == NULL) {
00255     return OMX_ErrorBadParameter;
00256   }
00257   DEBUG(DEB_LEV_SIMPLE_SEQ, "   Getting parameter %i\n", nParamIndex);
00258   /* Check which structure we are being fed and fill its header */
00259   switch(nParamIndex) {
00260     case OMX_IndexParamAudioInit:
00261       if ((err = checkHeader(ComponentParameterStructure, sizeof(OMX_PORT_PARAM_TYPE))) != OMX_ErrorNone) { 
00262         break;
00263       }
00264       memcpy(ComponentParameterStructure, &omx_volume_component_Private->sPortTypesParam[OMX_PortDomainAudio], sizeof(OMX_PORT_PARAM_TYPE));
00265       break;    
00266     case OMX_IndexParamAudioPortFormat:
00267       pAudioPortFormat = (OMX_AUDIO_PARAM_PORTFORMATTYPE*)ComponentParameterStructure;
00268       if ((err = checkHeader(ComponentParameterStructure, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE))) != OMX_ErrorNone) { 
00269         break;
00270       }
00271       if (pAudioPortFormat->nPortIndex <= 1) {
00272         port= (omx_base_audio_PortType *)omx_volume_component_Private->ports[pAudioPortFormat->nPortIndex];
00273         memcpy(pAudioPortFormat, &port->sAudioParam, sizeof(OMX_AUDIO_PARAM_PORTFORMATTYPE));
00274       } else {
00275         err = OMX_ErrorBadPortIndex;
00276       }
00277     break;    
00278     case OMX_IndexParamAudioPcm:
00279       pAudioPcmMode = (OMX_AUDIO_PARAM_PCMMODETYPE*)ComponentParameterStructure;
00280       if ((err = checkHeader(ComponentParameterStructure, sizeof(OMX_AUDIO_PARAM_PCMMODETYPE))) != OMX_ErrorNone) { 
00281         break;
00282       }
00283 
00284       if (pAudioPcmMode->nPortIndex > 1) {
00285         return OMX_ErrorBadPortIndex;
00286       }
00287       pAudioPcmMode->nChannels = 2;
00288       pAudioPcmMode->eNumData = OMX_NumericalDataSigned;
00289       pAudioPcmMode->eEndian = OMX_EndianBig;
00290       pAudioPcmMode->bInterleaved = OMX_TRUE;
00291       pAudioPcmMode->nBitPerSample = 16;
00292       pAudioPcmMode->nSamplingRate = 0;
00293       pAudioPcmMode->ePCMMode = OMX_AUDIO_PCMModeLinear;
00294       break;
00295     default:
00296       err = omx_base_component_GetParameter(hComponent, nParamIndex, ComponentParameterStructure);
00297   }
00298   return err;
00299 }

Generated for OpenMAX Bellagio rel. 0.9.0 by  doxygen 1.5.1
SourceForge.net Logo