#include "speech_encoder.h"
#include "copy.h"
#include "ssc_modes.h"
#include "basic_op.h"
#include "math_op.h"
#include "rc_encode.h"
#include "speech_mdct.h"
#include "bands.h"
#include "ssc_quant.h"
#include "ssc_pulsealloc.h"
#include "pitch.h"
#include "comb_filter.h"






//extern int count;


#define SYNCWORD 0xF7


#ifdef HW_DATASIZE
int pre_shared[360];   //
#endif


#ifdef OPT_DATASIZE2
int shared2[240];
#endif



short speech_encoder_get_size()
{
#ifndef OPT_DATASIZE2
	return sizeof(SPEECHEncoder)+ 2496;
#else
	return sizeof(SPEECHEncoder)+ 800;
#endif
}



static int sun_custom_encoder_init_arch(SPEECHEncoder *st, const SpeechMode *mode)
{
   if (st==NULL || mode==NULL) return SUN_ALLOC_FAIL;
   SUN_CLEAR_uchar((unsigned char*)st, speech_encoder_get_size());
   st->mode = mode;

#ifndef OPT_DATASIZE2
   st->overlap = mode->overlap;
   st->end = st->mode->nbEBands;

   st->delayedIntra = 1;
#endif


   return SUN_OK;
}



static short alloc_trim_analysis(const short *bandLogE, short end)
{
	short i;
	int diff=0;
	short trim = 1280;

	for (i=0;i<end-1;i++)
	{

#ifndef HW_HIFI3
         diff += bandLogE[i]*(int)(2+2*i-end);
#else
         diff += SSC_MULT16x16(bandLogE[i],(2+(i<<1)-end));
#endif

	}
	diff /= (end-1);

	trim -= SPEECH_MAX(-512, SPEECH_MIN(512, SHR(diff+1024,2)/6 ));

	return SHR(trim+128, 8);

}







#ifndef HW_DATASIZE
short pitch_buf[156];	//(MAX_PITCH_PERIOD+N)>>1
#endif

static short pitch_filter_enc(SPEECHEncoder *speech_enc_hnd, int *in, int *prefilter_mem, short N, short *pitch, short *gain, short *qgain, short nbAvailableBytes)
{

	short downsampled_buf[156];

	short pitch_gain;
	short pitch_threshold;
	short pitch_enable;
	short p_idx;
	short quantized_gain;
	short overlap = 120;

	
//	printf("run prefilter_overlap = %d\n",overlap);



#ifndef HW_PITCH_OFF2

    



	SUN_COPY_32(pre_shared, prefilter_mem, MAX_PITCH_PERIOD);
	SUN_COPY_32(pre_shared+MAX_PITCH_PERIOD, in+overlap, N);
	downsample_enc(pre_shared , downsampled_buf, MAX_PITCH_PERIOD+N);


	/////////////////////////////////// OK ////////////////////////////////////
	//	energy_test(downsampled_buf,downsampled_buf,downsampled_buf+60,&result1, &result2);
	//	printf("result1 = %d, result2 = %d\n",result1,result2);

	///////////////////////////////////////////////////////////////////////////////

#ifndef HW_HIFI3
	search_pitch_candidate(downsampled_buf+(MAX_PITCH_PERIOD>>1), downsampled_buf, N,
		MAX_PITCH_PERIOD-3*MIN_PITCH_PERIOD, &p_idx);
#else
	search_pitch_candidate(downsampled_buf+(MAX_PITCH_PERIOD>>1), downsampled_buf, N,
		MAX_PITCH_PERIOD-SSC_MULT16x16(3,MIN_PITCH_PERIOD), &p_idx);
#endif


	p_idx = MAX_PITCH_PERIOD-p_idx;


	/////////////////////////////// OK //////////////////////////////////
	//	energy_test(downsampled_buf,downsampled_buf,downsampled_buf+60,&result1, &result2);
	//	printf("result1 = %d, result2 = %d\n",result1,result2);

	///////////////////////////////////////////////////////////////////////////////

#ifndef HW_CODESIZE
	pitch_gain = no_doubling_get_gain(downsampled_buf, MAX_PITCH_PERIOD, MIN_PITCH_PERIOD,
			N, &p_idx, speech_enc_hnd->prefilter_period, speech_enc_hnd->prefilter_gain);
#else

	pitch_gain = no_doubling_get_gain(downsampled_buf, MAX_PITCH_PERIOD, MIN_PITCH_PERIOD,
			 &p_idx, speech_enc_hnd->prefilter_period, speech_enc_hnd->prefilter_gain);
#endif

	/////////////////////////////// OK //////////////////////////////////
	//	energy_test(downsampled_buf,downsampled_buf,downsampled_buf+60,&result1, &result2);
	//	printf("result1 = %d, result2 = %d\n",result1,result2);

	///////////////////////////////////////////////////////////////////////////////

	if (p_idx > MAX_PITCH_PERIOD-2)
			p_idx = MAX_PITCH_PERIOD-2;

	pitch_gain = SSC_MULT16x16_Q15(22938,pitch_gain);
  
	/* Adjusting the threshold based on rate and continuity */
	pitch_threshold = 9821;
#ifndef HW_HIFI3
	if (ABS(p_idx-speech_enc_hnd->prefilter_period)*10>p_idx) pitch_threshold += 6554;
#else
	if (SSC_MULT16x16(ABS(p_idx-speech_enc_hnd->prefilter_period),10)>p_idx) pitch_threshold += 6554;
#endif
	if (nbAvailableBytes<25) pitch_threshold += 3277;
	if (nbAvailableBytes<35) pitch_threshold += 3277;
	if (speech_enc_hnd->prefilter_gain > 13107) pitch_threshold -= 3277;
	if (speech_enc_hnd->prefilter_gain > 18022) pitch_threshold -= 3277;

	/* Hard threshold at 0.2 */
	pitch_threshold = SPEECH_MAX(pitch_threshold, 6554);



#ifdef HW_PITCH_OFF
	if(1)
#else
	if (pitch_gain<pitch_threshold)
#endif
	{
		pitch_gain = 0;
		pitch_enable = 0;
		quantized_gain = 0;
	} 
	else 
	{
		/*This block is not gated by a total bits check only because
		of the nbAvailableBytes check above.*/
		if (ABS(pitch_gain-speech_enc_hnd->prefilter_gain)<3277)  
			pitch_gain=speech_enc_hnd->prefilter_gain;
		
		quantized_gain = ((pitch_gain+1536)>>10)/3-1;
		quantized_gain = SPEECH_MAX(0, SPEECH_MIN(7, quantized_gain));
#ifndef HW_HIFI3
		pitch_gain = 3072*(quantized_gain+1);
#else
		pitch_gain = SSC_MULT16x16(3072,(quantized_gain+1));
#endif
		pitch_enable = 1;
	}
	speech_enc_hnd->prefilter_period=SPEECH_MAX(speech_enc_hnd->prefilter_period, MIN_PITCH_PERIOD);

#endif


#ifndef OPT_DATASIZE3
	SUN_COPY_32(in, (int *)speech_enc_hnd->in_mem, speech_enc_hnd->overlap);
#endif
	
#ifndef HW_PITCH_OFF2

#ifndef HW_SAVESTACK
	if(speech_enc_hnd->prefilter_gain==0 && pitch_gain==0) 
	{
	//	SUN_COPY_32(in+speech_enc_hnd->overlap, pre_shared+MAX_PITCH_PERIOD, N);
		SUN_COPY_32(in+overlap, pre_shared+MAX_PITCH_PERIOD, N);
	}
	else
	{
/*
		comb_filter(in+speech_enc_hnd->overlap, pre_shared+MAX_PITCH_PERIOD,
			speech_enc_hnd->prefilter_period, p_idx, N, -speech_enc_hnd->prefilter_gain, -pitch_gain, speech_enc_hnd->overlap);
*/
		comb_filter(in+overlap, pre_shared+MAX_PITCH_PERIOD,
			speech_enc_hnd->prefilter_period, p_idx, N, -speech_enc_hnd->prefilter_gain, -pitch_gain, overlap);
	}

#else
	if(speech_enc_hnd->prefilter_gain!=0 && pitch_gain!=0) 
	{
		comb_filter(in+speech_enc_hnd->overlap, in+speech_enc_hnd->overlap, speech_enc_hnd->prefilter_period, p_idx, N, -speech_enc_hnd->prefilter_gain, -pitch_gain, speech_enc_hnd->overlap);
	}
#endif


#endif
#ifndef OPT_DATASIZE3
	SUN_COPY_32((int *)speech_enc_hnd->in_mem, in+N, speech_enc_hnd->overlap);
#endif

	SUN_COPY_32(prefilter_mem, prefilter_mem+N, MAX_PITCH_PERIOD-N);

#ifndef HW_PITCH_OFF2

	SUN_COPY_32(prefilter_mem+MAX_PITCH_PERIOD-N, pre_shared+MAX_PITCH_PERIOD, N);




#endif


#ifndef HW_PITCH_OFF2
	*gain = pitch_gain;
	*pitch = p_idx;
	*qgain = quantized_gain;

	return pitch_enable;
#else
	return 0;
#endif

	
};








#ifndef HW_30BYTE
short speech_encode_inside(void *_st, short * pcm, short frame_size, unsigned char *compressed, short nbCompressedBytes)
#else
short speech_encode(void *_st, short * pcm, short frame_size, unsigned char *compressed, short nbCompressedBytes)
#endif
{
	short i, N, bits;
	SPEECHEncoder *st = (SPEECHEncoder *)_st;
#ifndef OPT_DATASIZE2
	short speech_ptr =  (frame_size<<2)+((st->end)<<1)+(MAX_PITCH_PERIOD<<2) ; 
#else
	short speech_ptr2 = 0;
#endif

	int *in;  
	int *freq;
	short *X  ;
	int *bandE;
	short *bandLogE;
	short *error;
	short *fine_quant; 
	short *pulses;
	const short *cap;
	short *fine_priority;
	short *oldBandE;
	short pf_on;
	short qg;
	short pitch_index=MIN_PITCH_PERIOD;
	short gain1;
	int *prefilter_mem;
	short tell;
	short balance;
	short total_bits;
	short silence=0;
	short nbAvailableBytes;
	short effEnd = 16;
	short codedBands;
	short alloc_trim;
	short sample_max;
	short overlap;
	const SpeechMode *mode;
	ec_enc encode;
	ec_enc *enc=&encode;

	
#ifdef OPT_DATASIZE2
	char *shared_char = (char *)pre_shared;
	short *offsets;

#ifdef HW_EXPROTATION_LAST
	short *exp_flag;
#endif

#else
	short offsets[16];

#ifdef HW_EXPROTATION_LAST
	short exp_flag[3];
#endif

#endif


#ifndef OPT_DATASIZE3
	int *in_mem = (int *)st->in_mem;
#endif


#ifndef OPT_DATASIZE2
	in = (int *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((frame_size)<<2);
	X = (short *)(st->in_mem + speech_ptr);
	freq = (int *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((frame_size)<<2);
#else
//	in = (int *)(shared2);
//	X = (short *)(shared2);
//	freq = (int *)(shared2);
	in = (int *)(shared2);
	X = (short *)(&shared2[120]);
	freq = (int *)(&shared2[120]);
#endif



#ifndef OPT_DATASIZE2
	bandE = (int *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<2);
	bandLogE = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
	fine_quant = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
	error = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
	pulses = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
	cap = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
	fine_priority = (short *)(st->in_mem + speech_ptr);
	speech_ptr = speech_ptr+((st->end)<<1);
#else
	bandE = (int *)(shared_char+speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<2);
	bandLogE = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);
	fine_quant = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);
	error = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);
	pulses = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);
	cap = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);
	fine_priority = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+((effEnd)<<1);

	offsets = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+(16<<1);

	exp_flag = (short *)(shared_char + speech_ptr2);
	speech_ptr2 = speech_ptr2+(3<<1);
#endif





	mode = st->mode;
	overlap = mode->overlap;
	if (nbCompressedBytes<2 || pcm==NULL) return SUN_BAD_ARG;
	N = frame_size;



	//�뚧�耶쀧Е
	*compressed = SYNCWORD;

#ifndef CRC_ON_8BIT
	compressed++;	
	nbCompressedBytes--;
#else
	compressed = compressed+2;
	nbCompressedBytes = nbCompressedBytes - 2;
#endif

#ifndef HW_CODESIZE
	rc_enc_init_1(enc, compressed, nbCompressedBytes);
	enc->storage=nbCompressedBytes;
#else
	enc->buf=compressed;
	enc->end_offs=0;         /////////////////////////////////
	enc->end_window=0;       /////////////////////////////
	enc->nend_bits=0;           ////////////////////////////
	/*This is the offset from which ec_tell() will subtract partial bits.*/
	enc->nbits_total=EC_CODE_BITS+1;
	enc->offs=0;

//	_this->rng=0x80000000;      //
	enc->rng=EC_CODE_TOP+1;  // 24bit coding

	enc->rem=-1;     ///////////////////////////////////////////
	enc->val=0;                 ////////////////////////////
	enc->ext=0;                  ////////////////////////
	enc->storage=nbCompressedBytes;         ////////////////////////
	enc->error=0;              /////////////////
#endif



#ifndef OPT_DATASIZE3
	prefilter_mem = in_mem+st->overlap;
#else
	prefilter_mem = (int *)st->in_mem;
#endif

	oldBandE = (short*)(prefilter_mem+MAX_PITCH_PERIOD); 

	tell=ec_tell(enc);

	/* Can't produce more than 1275 output bytes */

	nbAvailableBytes = nbCompressedBytes;
	total_bits = nbCompressedBytes<<3;

/*
	effEnd = st->end;
*/
	sample_max= st->overlap_max;
#ifndef Merge_max_copy_opt_hifi3_ZH
	st->overlap_max=speech_maxabs16(pcm, (int)overlap);
#else
	st->overlap_max=speech_maxabs16_copy(pcm, (int)overlap,in+overlap);
#endif
	sample_max=SPEECH_MAX(sample_max, st->overlap_max);

#ifndef HW_SILENCE_1
	silence = (sample_max==0);
#else
	silence = (sample_max<2);
#endif

	if (tell==1)
		rc_enc_bit_logp_1(enc, silence, 15);
	else
      silence=0;

	if (silence)
	{  
#ifndef HW_HIFI3
		tell = nbCompressedBytes*8;
#else
		tell = nbCompressedBytes<<3;
#endif
		enc->nbits_total+=tell-ec_tell(enc);


#ifdef HW_SILENCE_ENC


#ifdef CRC_ON_8BIT
		compressed = compressed-2;
		compressed[1] = 0xb6;
		compressed[2] = 0xff;
		compressed[3] = 0xfe;

#else
#ifdef CRC_ON_4BIT
		compressed = compressed-1;
		compressed[0] = 0xa0;
		compressed[1] = 0xff;
		compressed[2] = 0xfe;
		compressed[3] = 0x00;
#else
		compressed = compressed-1;
		compressed[1] = 0xff;
		compressed[2] = 0xfe;
		compressed[3] = 0x00;
#endif
#endif


		oldBandE[0] = -28672;
		oldBandE[1] = -28672;
		oldBandE[2] = -28672;
		oldBandE[3] = -28672;

		for(i=4; i<16; i++)
		{
			compressed[i] = 0x00;
			oldBandE[i] = -28672;
		}

		for(i=16; i<28; i++)
		{
			compressed[i] = 0x00;
		}


		for(i=0; i<N; i++)
		{
#ifndef OPT_DATASIZE2
			in_mem[i] = (((int)pcm[i]) << 4);
#else
			in[i] = (((int)pcm[i]) << 4);
#endif
		}

		SUN_COPY_32(prefilter_mem, prefilter_mem+N, MAX_PITCH_PERIOD-N);

		codedBands = 1;
		if (st->lastCodedBands)
			st->lastCodedBands = SPEECH_MIN(st->lastCodedBands+1,SPEECH_MAX(st->lastCodedBands-1,codedBands));
		else
			st->lastCodedBands = codedBands;

		return (nbCompressedBytes+2);
#endif


	}

//	printf("N = %d\n",N);
//	printf("1_overlap = %d\n",overlap);
#ifndef Merge_max_copy_opt_hifi3_ZH
#ifndef  speech_encode_loop_opt_hifi3_ZH
	for(i=0; i<N; i++)
	{
//		*(in+st->overlap+i) = pcm[i] << 4;
		*(in+overlap+i) = pcm[i] << 4;
	}
#else
	{
	  ae_int16x4  X, *ptx, *pty;
	  ae_int32x2  tmpL1, tmpL2;
	  ptx = (ae_int16x4 *)&pcm[0];
	  pty = (ae_int32x2 *)(in+overlap);
	  for(i=0; i<N; i+=4 )
	  {
		  AE_L16X4_IP(X, ptx, 8);
		  tmpL1 = AE_CVT32X2F16_32(X);
		  tmpL2 = AE_CVT32X2F16_10(X);
		  tmpL1 = AE_SRAI32(tmpL1, 12);
		  tmpL2 = AE_SRAI32(tmpL2, 12);
		  AE_S32X2_IP(tmpL1, pty, 8);
		  AE_S32X2_IP(tmpL2, pty, 8);
	  }
	}
#endif
#endif
   

//	printf("2_overlap = %d\n",overlap);

	pf_on = pitch_filter_enc(st, in, prefilter_mem, N, &pitch_index,&gain1, &qg, nbAvailableBytes);

#ifndef HW_PITCH_OFF2
	if (pf_on==0)
	{
		if(tell+16<=total_bits) rc_enc_bit_logp_1(enc, 0, 1);
	}
	else
	{
		short octave;
		rc_enc_bit_logp_1(enc, 1, 1);
		pitch_index += 1;
#ifndef HW_CODESIZE
		octave = EC_ILOG(pitch_index)-5;
#else
#ifndef VC_PROJ
		octave = (26-AE_NSAZ32_L(pitch_index));
#else
		octave = EC_ILOG(pitch_index)-5;
#endif
#endif
		rc_enc_uint_1(enc, octave, 6);
		rc_enc_bits_1(enc, pitch_index-(16<<octave), 4+octave);
		pitch_index -= 1;
		rc_enc_bits_1(enc, qg, 3);
	}
#else
	if(tell+16<=total_bits) 
		rc_enc_bit_logp_1(enc, 0, 1);
#endif



#ifdef OPT_DATASIZE3
	SUN_COPY_32((&pre_shared[240]), in+N, overlap);
#endif

	clt_mdct_forward(mode->trig, in, freq);


#ifdef OPT_DATASIZE3
	SUN_COPY_32(in, (&pre_shared[240]), overlap);
#endif


//	compute_band_energies(mode, freq, bandE, st->end);
	compute_band_energies(mode, freq, bandE, effEnd);
	amp2Log2(mode, effEnd, bandE, bandLogE);


	/* Band normalisation */
	normalise_bands(mode, freq, X, bandE, effEnd);

#ifdef HW_DEBUG_TABLE_INPUT
	for (i=0;i<st->end;i++)
		oldBandE[i]=0;
#endif


//	max_decay = SHL(EXTEND32(nbAvailableBytes),7);
#ifndef HW_CODESIZE
	quant_coarse_energy(mode, effEnd, bandLogE,
		oldBandE, total_bits, error, enc, nbAvailableBytes, &st->delayedIntra);
#else
	quant_coarse_energy(mode, effEnd, bandLogE,
			oldBandE, total_bits, error, enc, SHL(EXTEND32(nbAvailableBytes),7));
#endif

	cap = _cap;
	total_bits<<=BITRES;
	tell = ec_tell_frac(enc);
	alloc_trim = 5;
	if (tell+48 <= total_bits)
	{
//		alloc_trim = alloc_trim_analysis(bandLogE, st->end);
		alloc_trim = alloc_trim_analysis(bandLogE, effEnd);

		rc_enc_icdf_1(enc, alloc_trim, trim_icdf, 7);
		tell = ec_tell_frac(enc);
	}
	bits = (nbCompressedBytes<<6) - ec_tell_frac(enc) - 1;


	compute_offset(oldBandE,offsets);
	//codedBands = calculate_bitalloc(mode, st->end,  offsets, cap, alloc_trim, bits, &balance, pulses,fine_quant, fine_priority, enc, st->lastCodedBands);
	codedBands = calculate_bitalloc(mode, effEnd,  offsets, cap, alloc_trim, bits, &balance, pulses,fine_quant, fine_priority, enc, st->lastCodedBands);



	if (st->lastCodedBands)
		st->lastCodedBands = SPEECH_MIN(st->lastCodedBands+1,SPEECH_MAX(st->lastCodedBands-1,codedBands));
	else
		st->lastCodedBands = codedBands;




//	quant_fine_energy(mode, st->end, oldBandE, error, fine_quant, enc);
	quant_fine_energy(mode, effEnd, oldBandE, error, fine_quant, enc);
#ifndef HW_EXPROTATION_LAST
	quant_all_bands(mode, st->end, X, pulses, nbCompressedBytes<<6, balance, enc, codedBands, &st->rng);
#else
	exp_flag[0] = oldBandE[13]<7800;
	exp_flag[1] = oldBandE[14]<7800;
	exp_flag[2] = oldBandE[15]<7800;





#ifndef HW_BANDLIMIT
//	quant_all_bands(mode, st->end, X, pulses, nbCompressedBytes<<6, balance, enc, codedBands, exp_flag);
	quant_all_bands(mode, effEnd, X, pulses, nbCompressedBytes<<6, balance, enc, codedBands, exp_flag);
#else
	quant_all_bands(mode, bandlimit, X, pulses, nbCompressedBytes<<6, balance, enc, codedBands, &st->rng, exp_flag);
#endif


#endif

	
//	quant_energy_finalise(mode, st->end, oldBandE, error, fine_quant, fine_priority, (nbCompressedBytes<<3)-ec_tell(enc), enc);
	quant_energy_finalise(mode, effEnd, oldBandE, error, fine_quant, fine_priority, (nbCompressedBytes<<3)-ec_tell(enc), enc);
#ifndef HW_PITCH_OFF2
	st->prefilter_period = pitch_index;
	st->prefilter_gain = gain1;
#endif

	if (silence)
	{
		for (i=0;i<(mode->nbEBands);i++)
			oldBandE[i] = -28672;
	}
	st->rng = enc->rng;

	/* If there's any room left (can only happen for very high rates),
		it's already filled with zeros */
	rc_enc_done_1(enc);

	////////////  CRC �ｌ쓣 怨�////////////
	// compressed - 1 ���섏쐞 4BIT��CRC瑜��ｋ뒗��

#ifdef CRC_ON_4BIT
	UpdateCRC4(compressed - 1);
#endif


#ifdef CRC_ON_8BIT
	*(compressed-1) = UpdateCRC8(compressed-1);
#endif

	//
	/////////////////////////////////////
	if (ec_get_error(enc))
		return SUN_INTERNAL_ERROR;
	else
#ifndef CRC_ON_8BIT
		return (nbCompressedBytes+1);
#else
		return (nbCompressedBytes+2);
#endif
}


#ifndef HW_30BYTE
// 2媛쒖쓽 frame�먯꽌
//  (1) 2媛�frame 紐⑤몢 �뺤긽 : 56 return
//  (2) 泥ル쾲吏�frame留��깃났, �먮쾲吏�frame error ��寃쎌슦 : 28 return
//  (3) 泥ル쾲��frame �ㅽ뙣�쒕뒗 臾댁“嫄�: error code (<=0 ) return

short speech_encode(void *_st, short * pcm, short frame_size, unsigned char *compressed, short nbCompressedBytes)
{
	short ret1, ret2;
	ret1 = speech_encode_inside(_st, pcm, frame_size>>1, compressed, nbCompressedBytes>>1);

	if(ret1>0)
	{
		ret2 = speech_encode_inside(_st, pcm+(frame_size>>1), frame_size>>1, compressed+ret1, nbCompressedBytes>>1);
		if(ret2>0)
		{
			ret1 += ret2;
		}
	}
	return ret1;
}
#endif


void speech_encoder_init(void *enc, short Fs)
{
    SPEECHEncoder* speech_enc = (SPEECHEncoder *)enc;

#ifndef HW_DATA_SIZE_STATIC_MODES
	sun_custom_encoder_init_arch(speech_enc, &mode16000_160_40);
#else
	sun_custom_encoder_init_arch(speech_enc, ssc_custom_mode_create());
#endif

//    speech_enc->Fs = Fs;

}





