161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*--------------------------------------------------------------------------
361e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiCopyright (c) 2010-2014, The Linux Foundation. All rights reserved.
461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
561e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiRedistribution and use in source and binary forms, with or without
661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletimodification, are permitted provided that the following conditions are met:
761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    * Redistributions of source code must retain the above copyright
861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      notice, this list of conditions and the following disclaimer.
961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    * Redistributions in binary form must reproduce the above copyright
1061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      notice, this list of conditions and the following disclaimer in the
1161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      documentation and/or other materials provided with the distribution.
1261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    * Neither the name of The Linux Foundation nor
1361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      the names of its contributors may be used to endorse or promote
1461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      products derived from this software without specific prior written
1561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      permission.
1661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
1761e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1861e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1961e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiIMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2061e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiNON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2161e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiCONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2261e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiEXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2361e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiPROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2461e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2561e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiWHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2661e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2761e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti--------------------------------------------------------------------------*/
2961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
3061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
3161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*
3261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    An Open max test application ....
3361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti*/
3461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
3561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <stdio.h>
3661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <string.h>
3761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <stdlib.h>
3861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <unistd.h>
3961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <fcntl.h>
4061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <sys/types.h>
4161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <sys/mman.h>
4261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <time.h>
4361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <sys/ioctl.h>
4461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "OMX_Core.h"
4561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "OMX_Component.h"
4661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "pthread.h"
4761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <signal.h>
4861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
4961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <stdio.h>
5061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <stdlib.h>
5161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <fcntl.h>
5261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <stdint.h>
5361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <sys/mman.h>
5461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <sys/ioctl.h>
5561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include<unistd.h>
5661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include<string.h>
5761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <pthread.h>
5861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "QOMX_AudioExtensions.h"
5961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "QOMX_AudioIndexExtensions.h"
6061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#ifdef AUDIOV2
6161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include "control.h"
6261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#endif
6361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
6461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
6561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#include <linux/ioctl.h>
6661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
6761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletitypedef unsigned char uint8;
6861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletitypedef unsigned char byte;
6961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletitypedef unsigned int  uint32;
7061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletitypedef unsigned int  uint16;
7161e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiQOMX_AUDIO_STREAM_INFO_DATA streaminfoparam;
7261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/* maximum ADTS frame header length                */
7361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid Release_Encoder();
7461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
7561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#ifdef AUDIOV2
7661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiunsigned short session_id;
7761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint device_id;
7861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint control = 0;
7961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleticonst char *device="handset_tx";
8061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define DIR_TX 2
8161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#endif
8261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
8361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t samplerate = 8000;
8461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t channels = 1;
8561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t min_bitrate = 0;
8661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t max_bitrate = 0;
8761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t cdmarate = 0;
8861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t rectime = 0;
8961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t recpath = 0;
9061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t pcmplayback = 0;
9161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t tunnel      = 0;
9261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiuint32_t format = 1;
9361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define DEBUG_PRINT printf
9461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiunsigned to_idle_transition = 0;
9561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiunsigned long total_pcm_bytes;
9661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
9761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
9861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                GLOBAL INIT                    */
9961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
10061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
10161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
10261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                #DEFINES                            */
10361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
10461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define false 0
10561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define true 1
10661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
10761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define CONFIG_VERSION_SIZE(param) \
10861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    param.nVersion.nVersion = CURRENT_OMX_SPEC_VERSION;\
10961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    param.nSize = sizeof(param);
11061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
11161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define QCP_HEADER_SIZE sizeof(struct qcp_header)
11261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define MIN_BITRATE 4 /* Bit rate 1 - 13.6 , 2 - 6.2 , 3 - 2.7 , 4 - 1.0 kbps*/
11361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define MAX_BITRATE 4
11461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
11561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define FAILED(result) (result != OMX_ErrorNone)
11661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
11761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define SUCCEEDED(result) (result == OMX_ErrorNone)
11861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
11961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
12061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                GLOBAL DECLARATIONS                     */
12161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
12261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
12361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_mutex_t lock;
12461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_cond_t cond;
12561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_mutex_t elock;
12661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_cond_t econd;
12761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_cond_t fcond;
12861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_mutex_t etb_lock;
12961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_mutex_t etb_lock1;
13061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletipthread_cond_t etb_cond;
13161e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiFILE * inputBufferFile;
13261e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiFILE * outputBufferFile;
13361e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_PARAM_PORTDEFINITIONTYPE inputportFmt;
13461e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_PARAM_PORTDEFINITIONTYPE outputportFmt;
13561e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_AUDIO_PARAM_QCELP13TYPE qcelp13param;
13661e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_AUDIO_PARAM_PCMMODETYPE    pcmparam;
13761e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_PORT_PARAM_TYPE portParam;
13861e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_PORT_PARAM_TYPE portFmt;
13961e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_ERRORTYPE error;
14061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
14161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
14261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
14361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
14461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define ID_RIFF 0x46464952
14561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define ID_WAVE 0x45564157
14661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define ID_FMT  0x20746d66
14761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define ID_DATA 0x61746164
14861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
14961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#define FORMAT_PCM 1
15061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
15161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistruct wav_header {
15261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t riff_id;
15361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t riff_sz;
15461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t riff_fmt;
15561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t fmt_id;
15661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t fmt_sz;
15761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint16_t audio_format;
15861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint16_t num_channels;
15961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t sample_rate;
16061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t byte_rate;       /* sample_rate * num_channels * bps / 8 */
16161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint16_t block_align;     /* num_channels * bps / 8 */
16261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint16_t bits_per_sample;
16361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t data_id;
16461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  uint32_t data_sz;
16561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti};
16661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistruct enc_meta_out{
16761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int offset_to_frame;
16861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int frame_size;
16961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int encoded_pcm_samples;
17061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int msw_ts;
17161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int lsw_ts;
17261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int nflags;
17361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti} __attribute__ ((packed));
17461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
17561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistruct qcp_header {
17661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* RIFF Section */
17761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char riff[4];
17861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int s_riff;
17961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char qlcm[4];
18061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
18161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Format chunk */
18261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char fmt[4];
18361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int s_fmt;
18461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char mjr;
18561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char mnr;
18661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int data1;         /* UNIQUE ID of the codec */
18761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short data2;
18861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short data3;
18961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char data4[8];
19061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short ver;         /* Codec Info */
19161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        char name[80];
19261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short abps;    /* average bits per sec of the codec */
19361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short bytes_per_pkt;
19461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short samp_per_block;
19561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short samp_per_sec;
19661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short bits_per_samp;
19761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned char vr_num_of_rates;         /* Rate Header fmt info */
19861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned char rvd1[3];
19961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned short vr_bytes_per_pkt[8];
20061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int rvd2[5];
20161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
20261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Vrat chunk */
20361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned char vrat[4];
20461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int s_vrat;
20561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int v_rate;
20661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int size_in_pkts;
20761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
20861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Data chunk */
20961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned char data[4];
21061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        unsigned int s_data;
21161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti} __attribute__ ((packed));
21261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
21361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti /* Common part */
21461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti static struct qcp_header append_header = {
21561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti         {'R', 'I', 'F', 'F'}, 0, {'Q', 'L', 'C', 'M'},
21661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti         {'f', 'm', 't', ' '}, 150, 1, 0, 0, 0, 0,{0}, 0, {0},0,0,160,8000,16,0,{0},{0},{0},
21761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti         {'v','r','a','t'},0, 0, 0,{'d','a','t','a'},0
21861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti };
21961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
22061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int totaldatalen = 0;
22161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int framecnt = 0;
22261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
22361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                GLOBAL INIT                    */
22461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
22561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
22661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiunsigned int input_buf_cnt = 0;
22761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiunsigned int output_buf_cnt = 0;
22861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint used_ip_buf_cnt = 0;
22961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivolatile int event_is_done = 0;
23061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivolatile int ebd_event_is_done = 0;
23161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivolatile int fbd_event_is_done = 0;
23261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivolatile int etb_event_is_done = 0;
23361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint ebd_cnt;
23461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint bInputEosReached = 0;
23561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint bOutputEosReached = 0;
23661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint bInputEosReached_tunnel = 0;
23761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int etb_done = 0;
23861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint bFlushing = false;
23961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint bPause    = false;
24061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleticonst char *in_filename;
24161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleticonst char *out_filename;
24261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
24361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint timeStampLfile = 0;
24461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint timestampInterval = 100;
24561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
24661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti//* OMX Spec Version supported by the wrappers. Version = 1.1 */
24761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleticonst OMX_U32 CURRENT_OMX_SPEC_VERSION = 0x00000101;
24861e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_COMPONENTTYPE* qcelp13_enc_handle = 0;
24961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
25061e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_BUFFERHEADERTYPE  **pInputBufHdrs = NULL;
25161e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_BUFFERHEADERTYPE  **pOutputBufHdrs = NULL;
25261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
25361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
25461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                GLOBAL FUNC DECL                        */
25561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/************************************************************************/
25661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint Init_Encoder(char*);
25761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint Play_Encoder();
25861e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_STRING aud_comp;
25961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/**************************************************************************/
26061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/*                STATIC DECLARATIONS                       */
26161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti/**************************************************************************/
26261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
26361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int open_audio_file ();
26461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int Read_Buffer(OMX_BUFFERHEADERTYPE  *pBufHdr );
26561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE Allocate_Buffer ( OMX_COMPONENTTYPE *qcelp13_enc_handle,
26661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       OMX_BUFFERHEADERTYPE  ***pBufHdrs,
26761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       OMX_U32 nPortIndex,
26861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       unsigned int bufCntMin, unsigned int bufSize);
26961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
27061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
27161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE EventHandler(OMX_IN OMX_HANDLETYPE hComponent,
27261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                  OMX_IN OMX_PTR pAppData,
27361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                  OMX_IN OMX_EVENTTYPE eEvent,
27461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                  OMX_IN OMX_U32 nData1, OMX_IN OMX_U32 nData2,
27561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                  OMX_IN OMX_PTR pEventData);
27661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE EmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
27761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                     OMX_IN OMX_PTR pAppData,
27861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
27961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
28061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE FillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
28161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                     OMX_IN OMX_PTR pAppData,
28261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                     OMX_IN OMX_BUFFERHEADERTYPE* pBuffer);
28361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE  parse_pcm_header();
28461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid wait_for_event(void)
28561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
28661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&lock);
28761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("%s: event_is_done=%d", __FUNCTION__, event_is_done);
28861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    while (event_is_done == 0) {
28961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_cond_wait(&cond, &lock);
29061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
29161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    event_is_done = 0;
29261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_unlock(&lock);
29361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
29461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
29561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid event_complete(void )
29661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
29761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&lock);
29861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (event_is_done == 0) {
29961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        event_is_done = 1;
30061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_cond_broadcast(&cond);
30161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
30261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_unlock(&lock);
30361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
30461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
30561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid etb_wait_for_event(void)
30661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
30761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&etb_lock1);
30861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("%s: etb_event_is_done=%d", __FUNCTION__, etb_event_is_done);
30961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    while (etb_event_is_done == 0) {
31061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_cond_wait(&etb_cond, &etb_lock1);
31161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
31261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    etb_event_is_done = 0;
31361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_unlock(&etb_lock1);
31461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
31561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
31661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid etb_event_complete(void )
31761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
31861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&etb_lock1);
31961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (etb_event_is_done == 0) {
32061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        etb_event_is_done = 1;
32161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_cond_broadcast(&etb_cond);
32261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
32361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_unlock(&etb_lock1);
32461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
32561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
32661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic void create_qcp_header(int Datasize, int Frames)
32761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
32861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.s_riff = (unsigned)(Datasize + (int)QCP_HEADER_SIZE - 8);
32961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* exclude riff id and size field */
33061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data1 = 0x5E7F6D41;
33161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data2 = 0xB115;
33261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data3 = 0x11D0;
33361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[0] = 0xBA;
33461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[1] = 0x91;
33561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[2] = 0x00;
33661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[3] = 0x80;
33761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[4] = 0x5F;
33861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[5] = 0xB4;
33961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[6] = 0xB9;
34061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.data4[7] = 0x7E;
34161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.ver = 0x0002;
34261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        memcpy(append_header.name, "Qcelp 13K", 9);
34361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.abps = 13000;
34461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.bytes_per_pkt = 35;
34561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.vr_num_of_rates = 5;
34661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.vr_bytes_per_pkt[0] = 0x0422;
34761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.vr_bytes_per_pkt[1] = 0x0310;
34861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.vr_bytes_per_pkt[2] = 0x0207;
34961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.vr_bytes_per_pkt[3] = 0x0103;
35061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.s_vrat = 0x00000008;
35161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.v_rate = 0x00000001;
35261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.size_in_pkts = (unsigned)Frames;
35361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        append_header.s_data = (unsigned)Datasize;
35461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return;
35561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
35661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
35761e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_ERRORTYPE EventHandler(OMX_IN OMX_HANDLETYPE hComponent,
35861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                           OMX_IN OMX_PTR pAppData,
35961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                           OMX_IN OMX_EVENTTYPE eEvent,
36061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                           OMX_IN OMX_U32 nData1, OMX_IN OMX_U32 nData2,
36161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                           OMX_IN OMX_PTR pEventData)
36261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
36361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Function %s \n", __FUNCTION__);
36461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
36561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* To remove warning for unused variable to keep prototype same */
36661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti   (void)hComponent;
36761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti   (void)pAppData;
36861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti   (void)pEventData;
36961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
37061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    switch(eEvent) {
37161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        case OMX_EventCmdComplete:
37261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n OMX_EventCmdComplete event=%d data1=%u data2=%u\n",(OMX_EVENTTYPE)eEvent,
37361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                                                               nData1,nData2);
37461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            event_complete();
37561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        break;
37661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        case OMX_EventError:
37761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n OMX_EventError \n");
37861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        break;
37961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti         case OMX_EventBufferFlag:
38061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti             DEBUG_PRINT("\n OMX_EventBufferFlag \n");
38161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti             bOutputEosReached = true;
38261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti             event_complete();
38361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti             break;
38461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        case OMX_EventPortSettingsChanged:
38561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n OMX_EventPortSettingsChanged \n");
38661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        break;
38761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        default:
38861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n Unknown Event \n");
38961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        break;
39061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
39161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return OMX_ErrorNone;
39261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
39361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
39461e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_ERRORTYPE FillBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
39561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                              OMX_IN OMX_PTR pAppData,
39661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                              OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
39761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
39861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    size_t bytes_writen = 0;
39961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    size_t total_bytes_writen = 0;
40061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    size_t len = 0;
40161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    struct enc_meta_out *meta = NULL;
40261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_U8 *src = pBuffer->pBuffer;
40361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    unsigned int num_of_frames = 1;
40461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
40561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* To remove warning for unused variable to keep prototype same */
40661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    (void)pAppData;
40761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
40861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(((pBuffer->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS)) {
40961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("FBD::EOS on output port\n ");
41061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bOutputEosReached = true;
41161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            return OMX_ErrorNone;
41261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
41361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(bInputEosReached_tunnel || bOutputEosReached)
41461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
41561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("EOS REACHED NO MORE PROCESSING OF BUFFERS\n");
41661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            return OMX_ErrorNone;
41761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
41861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(num_of_frames != src[0]){
41961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
42061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            printf("Data corrupt\n");
42161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            return OMX_ErrorNone;
42261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
42361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Skip the first bytes */
42461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
42561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
42661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
42761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        src += sizeof(unsigned char);
42861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        meta = (struct enc_meta_out *)src;
42961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        while (num_of_frames > 0) {
43061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            meta = (struct enc_meta_out *)src;
43161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            /*printf("offset=%d framesize=%d encoded_pcm[%d] msw_ts[%d]lsw_ts[%d] nflags[%d]\n",
43261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                                                       meta->offset_to_frame,
43361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                                                       meta->frame_size,
43461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                          meta->encoded_pcm_samples, meta->msw_ts, meta->lsw_ts, meta->nflags);*/
43561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            len = meta->frame_size;
43661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
43761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bytes_writen = fwrite(pBuffer->pBuffer + sizeof(unsigned char) + meta->offset_to_frame,1,len,outputBufferFile);
43861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            if(bytes_writen < len)
43961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            {
44061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                DEBUG_PRINT("error: invalid QCELP13 encoded data \n");
44161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                return OMX_ErrorNone;
44261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
44361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            src += sizeof(struct enc_meta_out);
44461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            num_of_frames--;
44561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            total_bytes_writen += len;
44661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
44761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT(" FillBufferDone size writen to file  %zu count %d\n",total_bytes_writen, framecnt);
44861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        totaldatalen = totaldatalen + (int)total_bytes_writen;
44961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    framecnt++;
45061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
45161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT(" FBD calling FTB\n");
45261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        OMX_FillThisBuffer(hComponent,pBuffer);
45361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
45461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return OMX_ErrorNone;
45561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
45661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
45761e21a4fe27118141afc13323807f83b733cf426Uday Kishore PasupuletiOMX_ERRORTYPE EmptyBufferDone(OMX_IN OMX_HANDLETYPE hComponent,
45861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                              OMX_IN OMX_PTR pAppData,
45961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                              OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
46061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
46161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    int readBytes =0;
46261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
46361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* To remove warning for unused variable to keep prototype same */
46461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    (void)pAppData;
46561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
46661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    ebd_cnt++;
46761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    used_ip_buf_cnt--;
46861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&etb_lock);
46961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(!etb_done)
47061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
47161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n*********************************************\n");
47261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Wait till first set of buffers are given to component\n");
47361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n*********************************************\n");
47461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        etb_done++;
47561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_mutex_unlock(&etb_lock);
47661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        etb_wait_for_event();
47761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
47861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else
47961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
48061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_mutex_unlock(&etb_lock);
48161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
48261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
48361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
48461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(bInputEosReached)
48561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
48661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n*********************************************\n");
48761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("   EBD::EOS on input port\n ");
48861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("*********************************************\n");
48961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return OMX_ErrorNone;
49061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }else if (bFlushing == true) {
49161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      DEBUG_PRINT("omx_qcelp13_adec_test: bFlushing is set to TRUE used_ip_buf_cnt=%d\n",used_ip_buf_cnt);
49261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      if (used_ip_buf_cnt == 0) {
49361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        bFlushing = false;
49461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      } else {
49561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("omx_qcelp13_adec_test: more buffer to come back used_ip_buf_cnt=%d\n",used_ip_buf_cnt);
49661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return OMX_ErrorNone;
49761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      }
49861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
49961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
50061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if((readBytes = Read_Buffer(pBuffer)) > 0) {
50161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pBuffer->nFilledLen = (OMX_U32)readBytes;
50261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        used_ip_buf_cnt++;
50361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        OMX_EmptyThisBuffer(hComponent,pBuffer);
50461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
50561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else{
50661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pBuffer->nFlags |= OMX_BUFFERFLAG_EOS;
50761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        used_ip_buf_cnt++;
50861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        bInputEosReached = true;
50961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pBuffer->nFilledLen = 0;
51061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        OMX_EmptyThisBuffer(hComponent,pBuffer);
51161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("EBD..Either EOS or Some Error while reading file\n");
51261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
51361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return OMX_ErrorNone;
51461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
51561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
51661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid signal_handler(int sig_id) {
51761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
51861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  /* Flush */
51961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  if (sig_id == SIGUSR1) {
52061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("%s Initiate flushing\n", __FUNCTION__);
52161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    bFlushing = true;
52261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_SendCommand(qcelp13_enc_handle, OMX_CommandFlush, OMX_ALL, NULL);
52361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  } else if (sig_id == SIGUSR2) {
52461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (bPause == true) {
52561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      DEBUG_PRINT("%s resume record\n", __FUNCTION__);
52661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      bPause = false;
52761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
52861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    } else {
52961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      DEBUG_PRINT("%s pause record\n", __FUNCTION__);
53061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      bPause = true;
53161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StatePause, NULL);
53261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
53361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti  }
53461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
53561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
53661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint main(int argc, char **argv)
53761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
53861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti     unsigned int bufCnt=0;
53961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti     OMX_ERRORTYPE result;
54061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
54161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    struct sigaction sa;
54261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
54361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    memset(&sa, 0, sizeof(sa));
54461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    sa.sa_handler = &signal_handler;
54561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    sigaction(SIGABRT, &sa, NULL);
54661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    sigaction(SIGUSR1, &sa, NULL);
54761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    sigaction(SIGUSR2, &sa, NULL);
54861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
54961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti   (void) signal(SIGINT, Release_Encoder);
55061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
55161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_cond_init(&cond, 0);
55261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_init(&lock, 0);
55361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_cond_init(&etb_cond, 0);
55461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_init(&etb_lock, 0);
55561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_init(&etb_lock1, 0);
55661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
55761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (argc >= 9) {
55861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        in_filename = argv[1];
55961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          out_filename = argv[2];
56061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    tunnel =  (uint32_t)atoi(argv[3]);
56161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        min_bitrate  = (uint32_t)atoi(argv[4]);
56261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        max_bitrate  = (uint32_t)atoi(argv[5]);
56361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        cdmarate     = (uint32_t)atoi(argv[6]);
56461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        recpath      = (uint32_t)atoi(argv[7]); // No configuration support yet..
56561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        rectime      = (uint32_t)atoi(argv[8]);
56661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
56761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    } else {
56861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT(" invalid format: \n");
56961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("ex: ./mm-aenc-omxqcelp13-test INPUTFILE OUTPUTFILE Tunnel MINRATE MAXRATE CDMARATE RECORDPATH RECORDTIME\n");
57061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("MINRATE, MAXRATE and CDMARATE 1 to 4\n");
57161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("RECORDPATH 0(TX),1(RX),2(BOTH),3(MIC)\n");
57261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("RECORDTIME in seconds for AST Automation\n");
57361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          return 0;
57461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
57561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(recpath != 3) {
57661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("For RECORDPATH Only MIC supported\n");
57761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          return 0;
57861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
57961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
58061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(tunnel == 0)
58161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        aud_comp = "OMX.qcom.audio.encoder.qcelp13";
58261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else
58361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        aud_comp = "OMX.qcom.audio.encoder.tunneled.qcelp13";
58461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(Init_Encoder(aud_comp)!= 0x00)
58561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
58661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Decoder Init failed\n");
58761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
58861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
58961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
59061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    fcntl(0, F_SETFL, O_NONBLOCK);
59161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
59261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(Play_Encoder() != 0x00)
59361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
59461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Play_Decoder failed\n");
59561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
59661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
59761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
59861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    // Wait till EOS is reached...
59961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(rectime && tunnel)
60061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
60161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            sleep(rectime);
60261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            rectime = 0;
60361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bInputEosReached_tunnel = 1;
60461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("\EOS ON INPUT PORT\n");
60561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
60661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        else
60761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
60861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            wait_for_event();
60961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
61061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
61161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if((bInputEosReached_tunnel) || ((bOutputEosReached) && !tunnel))
61261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
61361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
61461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("\nMoving the decoder to idle state \n");
61561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StateIdle,0);
61661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            wait_for_event();
61761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
61861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("\nMoving the encoder to loaded state \n");
61961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StateLoaded,0);
62061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            sleep(1);
62161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            if (!tunnel)
62261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            {
62361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                DEBUG_PRINT("\nFillBufferDone: Deallocating i/p buffers \n");
62461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                for(bufCnt=0; bufCnt < input_buf_cnt; ++bufCnt) {
62561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                    OMX_FreeBuffer(qcelp13_enc_handle, 0, pInputBufHdrs[bufCnt]);
62661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                }
62761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
62861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
62961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT ("\nFillBufferDone: Deallocating o/p buffers \n");
63061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            for(bufCnt=0; bufCnt < output_buf_cnt; ++bufCnt) {
63161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                OMX_FreeBuffer(qcelp13_enc_handle, 1, pOutputBufHdrs[bufCnt]);
63261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
63361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            wait_for_event();
63461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            create_qcp_header(totaldatalen, framecnt);
63561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        fseek(outputBufferFile, 0,SEEK_SET);
63661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            fwrite(&append_header,1,QCP_HEADER_SIZE,outputBufferFile);
63761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
63861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
63961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            result = OMX_FreeHandle(qcelp13_enc_handle);
64061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            if (result != OMX_ErrorNone) {
64161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                DEBUG_PRINT ("\nOMX_FreeHandle error. Error code: %d\n", result);
64261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
64361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
64461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            /* Deinit OpenMAX */
64561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(tunnel)
64661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
64761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            #ifdef AUDIOV2
64861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            if (msm_route_stream(DIR_TX,session_id,device_id, 0))
64961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            {
65061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                DEBUG_PRINT("\ncould not set stream routing\n");
65161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                return -1;
65261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
65361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            if (msm_en_device(device_id, 0))
65461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            {
65561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                DEBUG_PRINT("\ncould not enable device\n");
65661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                return -1;
65761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            }
65861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            msm_mixer_close();
65961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            #endif
66061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
66161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            OMX_Deinit();
66261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            ebd_cnt=0;
66361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bOutputEosReached = false;
66461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bInputEosReached_tunnel = false;
66561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            bInputEosReached = 0;
66661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            qcelp13_enc_handle = NULL;
66761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            pthread_cond_destroy(&cond);
66861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            pthread_mutex_destroy(&lock);
66961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            fclose(outputBufferFile);
67061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("*****************************************\n");
67161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("******...QCELP13 ENC TEST COMPLETED...***************\n");
67261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("*****************************************\n");
67361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
67461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return 0;
67561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
67661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
67761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletivoid Release_Encoder()
67861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
67961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    static int cnt=0;
68061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_ERRORTYPE result;
68161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
68261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("END OF QCELP13 ENCODING: EXITING PLEASE WAIT\n");
68361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    bInputEosReached_tunnel = 1;
68461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    event_complete();
68561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    cnt++;
68661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(cnt > 1)
68761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
68861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* FORCE RESET  */
68961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        qcelp13_enc_handle = NULL;
69061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        ebd_cnt=0;
69161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        bInputEosReached_tunnel = false;
69261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
69361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        result = OMX_FreeHandle(qcelp13_enc_handle);
69461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if (result != OMX_ErrorNone) {
69561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT ("\nOMX_FreeHandle error. Error code: %d\n", result);
69661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
69761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
69861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Deinit OpenMAX */
69961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
70061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        OMX_Deinit();
70161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
70261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_cond_destroy(&cond);
70361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pthread_mutex_destroy(&lock);
70461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("*****************************************\n");
70561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("******...QCELP13 ENC TEST COMPLETED...***************\n");
70661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("*****************************************\n");
70761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        exit(0);
70861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
70961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
71061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
71161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint Init_Encoder(OMX_STRING audio_component)
71261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
71361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Inside %s \n", __FUNCTION__);
71461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_ERRORTYPE omxresult;
71561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_U32 total = 0;
71661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    typedef OMX_U8* OMX_U8_PTR;
71761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    char *role ="audio_encoder";
71861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
71961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    static OMX_CALLBACKTYPE call_back = {
72061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        &EventHandler,&EmptyBufferDone,&FillBufferDone
72161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    };
72261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
72361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Init. the OpenMAX Core */
72461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("\nInitializing OpenMAX Core....\n");
72561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    omxresult = OMX_Init();
72661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
72761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(OMX_ErrorNone != omxresult) {
72861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n Failed to Init OpenMAX core");
72961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          return -1;
73061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
73161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else {
73261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\nOpenMAX Core Init Done\n");
73361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
73461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
73561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Query for audio decoders*/
73661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Qcelp13_test: Before entering OMX_GetComponentOfRole");
73761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_GetComponentsOfRole(role, &total, 0);
73861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nTotal components of role=%s :%u", role, total);
73961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
74061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
74161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    omxresult = OMX_GetHandle((OMX_HANDLETYPE*)(&qcelp13_enc_handle),
74261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                        (OMX_STRING)audio_component, NULL, &call_back);
74361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (FAILED(omxresult)) {
74461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\nFailed to Load the component:%s\n", audio_component);
74561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
74661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
74761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else
74861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
74961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\nComponent %s is in LOADED state\n", audio_component);
75061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
75161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
75261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Get the port information */
75361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    CONFIG_VERSION_SIZE(portParam);
75461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    omxresult = OMX_GetParameter(qcelp13_enc_handle, OMX_IndexParamAudioInit,
75561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                (OMX_PTR)&portParam);
75661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
75761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(FAILED(omxresult)) {
75861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\nFailed to get Port Param\n");
75961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
76061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
76161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else
76261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
76361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\nportParam.nPorts:%u\n", portParam.nPorts);
76461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("\nportParam.nStartPortNumber:%u\n",
76561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                             portParam.nStartPortNumber);
76661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
76761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
76861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(OMX_ErrorNone != omxresult)
76961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
77061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Set parameter failed");
77161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
77261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
77361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return 0;
77461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
77561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
77661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiint Play_Encoder()
77761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
77861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    unsigned int i;
77961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    int Size=0;
78061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Inside %s \n", __FUNCTION__);
78161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_ERRORTYPE ret;
78261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_INDEXTYPE index;
78361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#ifdef __LP64__
78461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("sizeof[%ld]\n", sizeof(OMX_BUFFERHEADERTYPE));
78561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#else
78661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("sizeof[%d]\n", sizeof(OMX_BUFFERHEADERTYPE));
78761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti#endif
78861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
78961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* open the i/p and o/p files based on the video file format passed */
79061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(open_audio_file()) {
79161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n Returning -1");
79261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
79361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
79461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
79561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Query the encoder input min buf requirements */
79661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    CONFIG_VERSION_SIZE(inputportFmt);
79761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
79861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Port for which the Client needs to obtain info */
79961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    inputportFmt.nPortIndex = portParam.nStartPortNumber;
80061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
80161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_GetParameter(qcelp13_enc_handle,OMX_IndexParamPortDefinition,&inputportFmt);
80261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nEnc Input Buffer Count %u\n", inputportFmt.nBufferCountMin);
80361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nEnc: Input Buffer Size %u\n", inputportFmt.nBufferSize);
80461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
80561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(OMX_DirInput != inputportFmt.eDir) {
80661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nEnc: Expect Input Port\n");
80761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
80861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
80961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
81061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pcmparam.nPortIndex   = 0;
81161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pcmparam.nChannels    =  channels;
81261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pcmparam.nSamplingRate = samplerate;
81361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_SetParameter(qcelp13_enc_handle,OMX_IndexParamAudioPcm,&pcmparam);
81461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
81561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
81661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Query the encoder outport's min buf requirements */
81761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    CONFIG_VERSION_SIZE(outputportFmt);
81861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Port for which the Client needs to obtain info */
81961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    outputportFmt.nPortIndex = portParam.nStartPortNumber + 1;
82061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
82161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_GetParameter(qcelp13_enc_handle,OMX_IndexParamPortDefinition,&outputportFmt);
82261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nEnc: Output Buffer Count %u\n", outputportFmt.nBufferCountMin);
82361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nEnc: Output Buffer Size %u\n", outputportFmt.nBufferSize);
82461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
82561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(OMX_DirOutput != outputportFmt.eDir) {
82661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nEnc: Expect Output Port\n");
82761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
82861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
82961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
83061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
83161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    CONFIG_VERSION_SIZE(qcelp13param);
83261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
83361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    qcelp13param.nPortIndex   =  1;
83461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    qcelp13param.nChannels    =  channels; //2 ; /* 1-> mono 2-> stereo*/
83561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    qcelp13param.nMinBitRate = min_bitrate;
83661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    qcelp13param.nMaxBitRate = max_bitrate;
83761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_SetParameter(qcelp13_enc_handle,OMX_IndexParamAudioQcelp13,&qcelp13param);
83861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_GetExtensionIndex(qcelp13_enc_handle,"OMX.Qualcomm.index.audio.sessionId",&index);
83961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_GetParameter(qcelp13_enc_handle,index,&streaminfoparam);
84061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(tunnel) {
84161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    #ifdef AUDIOV2
84261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    session_id = streaminfoparam.sessionId;
84361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    control = msm_mixer_open("/dev/snd/controlC0", 0);
84461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(control < 0)
84561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    printf("ERROR opening the device\n");
84661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    device_id = msm_get_device(device);
84761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\ndevice_id = %d\n",device_id);
84861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("\nsession_id = %d\n",session_id);
84961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (msm_en_device(device_id, 1))
85061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
85161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        perror("could not enable device\n");
85261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
85361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
85461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (msm_route_stream(DIR_TX,session_id,device_id, 1))
85561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
85661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        perror("could not set stream routing\n");
85761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
85861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
85961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    #endif
86061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
86161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
86261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nOMX_SendCommand Encoder -> IDLE\n");
86361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StateIdle,0);
86461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* wait_for_event(); should not wait here event complete status will
86561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti       not come until enough buffer are allocated */
86661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (tunnel == 0)
86761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
86861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        input_buf_cnt = inputportFmt.nBufferCountActual; //  inputportFmt.nBufferCountMin + 5;
86961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Transition to Idle State succesful...\n");
87061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        /* Allocate buffer on decoder's i/p port */
87161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        error = Allocate_Buffer(qcelp13_enc_handle, &pInputBufHdrs, inputportFmt.nPortIndex,
87261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                            input_buf_cnt, inputportFmt.nBufferSize);
87361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if (error != OMX_ErrorNone || pInputBufHdrs == NULL) {
87461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT ("\nOMX_AllocateBuffer Input buffer error\n");
87561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
87661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
87761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else {
87861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_AllocateBuffer Input buffer success\n");
87961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
88061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
88161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    output_buf_cnt = outputportFmt.nBufferCountMin ;
88261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
88361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* Allocate buffer on encoder's O/Pp port */
88461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    error = Allocate_Buffer(qcelp13_enc_handle, &pOutputBufHdrs, outputportFmt.nPortIndex,
88561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                            output_buf_cnt, outputportFmt.nBufferSize);
88661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (error != OMX_ErrorNone || pOutputBufHdrs == NULL ) {
88761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_AllocateBuffer Output buffer error\n");
88861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return -1;
88961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
89061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else {
89161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_AllocateBuffer Output buffer success\n");
89261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
89361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
89461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    wait_for_event();
89561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
89661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
89761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (tunnel == 1)
89861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
89961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_SendCommand to enable TUNNEL MODE during IDLE\n");
90061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        OMX_SendCommand(qcelp13_enc_handle, OMX_CommandPortDisable,0,0); // disable input port
90161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        wait_for_event();
90261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
90361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
90461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT ("\nOMX_SendCommand encoder -> Executing\n");
90561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_SendCommand(qcelp13_enc_handle, OMX_CommandStateSet, OMX_StateExecuting,0);
90661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    wait_for_event();
90761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
90861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT(" Start sending OMX_FILLthisbuffer\n");
90961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
91061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    for(i=0; i < output_buf_cnt; i++) {
91161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_FillThisBuffer on output buf no.%d\n",i);
91261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pOutputBufHdrs[i]->nOutputPortIndex = 1;
91361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pOutputBufHdrs[i]->nFlags = pOutputBufHdrs[i]->nFlags & (unsigned)~OMX_BUFFERFLAG_EOS;
91461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        ret = OMX_FillThisBuffer(qcelp13_enc_handle, pOutputBufHdrs[i]);
91561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if (OMX_ErrorNone != ret) {
91661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("OMX_FillThisBuffer failed with result %d\n", ret);
91761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
91861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        else {
91961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("OMX_FillThisBuffer success!\n");
92061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
92161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
92261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
92361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletiif(tunnel == 0)
92461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
92561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT(" Start sending OMX_emptythisbuffer\n");
92661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    for (i = 0;i < input_buf_cnt;i++) {
92761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT ("\nOMX_EmptyThisBuffer on Input buf no.%d\n",i);
92861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pInputBufHdrs[i]->nInputPortIndex = 0;
92961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        Size = Read_Buffer(pInputBufHdrs[i]);
93061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(Size <=0 ){
93161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT("NO DATA READ\n");
93261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          bInputEosReached = true;
93361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          pInputBufHdrs[i]->nFlags= OMX_BUFFERFLAG_EOS;
93461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
93561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pInputBufHdrs[i]->nFilledLen = (OMX_U32)Size;
93661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        pInputBufHdrs[i]->nInputPortIndex = 0;
93761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        used_ip_buf_cnt++;
93861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        ret = OMX_EmptyThisBuffer(qcelp13_enc_handle, pInputBufHdrs[i]);
93961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if (OMX_ErrorNone != ret) {
94061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("OMX_EmptyThisBuffer failed with result %d\n", ret);
94161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
94261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        else {
94361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("OMX_EmptyThisBuffer success!\n");
94461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
94561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(Size <=0 ){
94661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            break;//eos reached
94761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
94861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
94961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_lock(&etb_lock);
95061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(etb_done)
95161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
95261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Component is waiting for EBD to be released.\n");
95361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        etb_event_complete();
95461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
95561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    else
95661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
95761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n****************************\n");
95861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("EBD not yet happened ...\n");
95961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n****************************\n");
96061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        etb_done++;
96161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
96261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pthread_mutex_unlock(&etb_lock);
96361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
96461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
96561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return 0;
96661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
96761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
96861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
96961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
97061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE Allocate_Buffer ( OMX_COMPONENTTYPE *avc_enc_handle,
97161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       OMX_BUFFERHEADERTYPE  ***pBufHdrs,
97261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       OMX_U32 nPortIndex,
97361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                       unsigned int bufCntMin, unsigned int bufSize)
97461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
97561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Inside %s \n", __FUNCTION__);
97661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    OMX_ERRORTYPE error=OMX_ErrorNone;
97761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    unsigned int bufCnt=0;
97861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
97961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    /* To remove warning for unused variable to keep prototype same */
98061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    (void)avc_enc_handle;
98161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
98261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    *pBufHdrs= (OMX_BUFFERHEADERTYPE **)
98361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                   malloc(sizeof(OMX_BUFFERHEADERTYPE*)*bufCntMin);
98461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
98561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    for(bufCnt=0; bufCnt < bufCntMin; ++bufCnt) {
98661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\n OMX_AllocateBuffer No %d \n", bufCnt);
98761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        error = OMX_AllocateBuffer(qcelp13_enc_handle, &((*pBufHdrs)[bufCnt]),
98861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                   nPortIndex, NULL, bufSize);
98961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
99061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
99161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return error;
99261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
99361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
99461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
99561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
99661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
99761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int Read_Buffer (OMX_BUFFERHEADERTYPE  *pBufHdr )
99861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
99961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
100061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    size_t bytes_read=0;
100161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
100261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
100361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pBufHdr->nFilledLen = 0;
100461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    pBufHdr->nFlags |= OMX_BUFFERFLAG_EOS;
100561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
100661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti     bytes_read = fread(pBufHdr->pBuffer, 1, pBufHdr->nAllocLen , inputBufferFile);
100761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
100861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      pBufHdr->nFilledLen = (OMX_U32)bytes_read;
100961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti      // Time stamp logic
101061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    ((OMX_BUFFERHEADERTYPE *)pBufHdr)->nTimeStamp = \
101161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
101261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    (OMX_TICKS) ((total_pcm_bytes * 1000)/(samplerate * channels *2));
101361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
101461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti       DEBUG_PRINT ("\n--time stamp -- %ld\n",  (unsigned long)((OMX_BUFFERHEADERTYPE *)pBufHdr)->nTimeStamp);
101561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(bytes_read == 0)
101661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
101761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          pBufHdr->nFlags |= OMX_BUFFERFLAG_EOS;
101861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti          DEBUG_PRINT ("\nBytes read zero\n");
101961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
102061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        else
102161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
102261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            pBufHdr->nFlags = pBufHdr->nFlags & (unsigned)~OMX_BUFFERFLAG_EOS;
102361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
102461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            total_pcm_bytes = (unsigned)(total_pcm_bytes + bytes_read);
102561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
102661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
102761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return (int)bytes_read;;
102861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
102961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
103061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
103161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
103261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti//In Encoder this Should Open a PCM or WAV file for input.
103361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
103461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic int open_audio_file ()
103561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
103661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    int error_code = 0;
103761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
103861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (!tunnel)
103961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
104061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Inside %s filename=%s\n", __FUNCTION__, in_filename);
104161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        inputBufferFile = fopen (in_filename, "rb");
104261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if (inputBufferFile == NULL) {
104361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("\ni/p file %s could NOT be opened\n",
104461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                         in_filename);
104561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        error_code = -1;
104661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
104761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        if(parse_pcm_header() != 0x00)
104861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        {
104961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            DEBUG_PRINT("PCM parser failed \n");
105061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti            return -1;
105161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        }
105261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
105361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
105461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Inside %s filename=%s\n", __FUNCTION__, out_filename);
105561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    outputBufferFile = fopen (out_filename, "wb");
105661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (outputBufferFile == NULL) {
105761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("\ni/p file %s could NOT be opened\n",
105861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                                         out_filename);
105961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    error_code = -1;
106061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return error_code;
106161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
106261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    fseek(outputBufferFile, QCP_HEADER_SIZE, SEEK_SET);
106361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return error_code;
106461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
106561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
106661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuletistatic OMX_ERRORTYPE parse_pcm_header()
106761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti{
106861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    struct wav_header hdr;
106961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
107061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("\n***************************************************************\n");
107161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if(fread(&hdr, 1, sizeof(hdr),inputBufferFile)!=sizeof(hdr))
107261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
107361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Wav file cannot read header\n");
107461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
107561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
107661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
107761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if ((hdr.riff_id != ID_RIFF) ||
107861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        (hdr.riff_fmt != ID_WAVE)||
107961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        (hdr.fmt_id != ID_FMT))
108061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
108161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Wav file is not a riff/wave file\n");
108261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
108361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
108461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
108561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    if (hdr.audio_format != FORMAT_PCM)
108661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    {
108761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        DEBUG_PRINT("Wav file is not adpcm format %d and fmt size is %d\n",
108861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti                      hdr.audio_format, hdr.fmt_sz);
108961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti        return -1;
109061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    }
109161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
109261e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Samplerate is %d\n", hdr.sample_rate);
109361e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("Channel Count is %d\n", hdr.num_channels);
109461e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    DEBUG_PRINT("\n***************************************************************\n");
109561e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
109661e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    samplerate = hdr.sample_rate;
109761e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    channels = hdr.num_channels;
109861e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    total_pcm_bytes = 0;
109961e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti
110061e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti    return OMX_ErrorNone;
110161e21a4fe27118141afc13323807f83b733cf426Uday Kishore Pasupuleti}
1102