Exemplo n.º 1
0
void blockEncoder(
		bool init,
		bool color_mode,
		int  sampling,
		unsigned short imagewidth,
		unsigned short imageheight,
		unsigned char Standard_Luminance_Quantization_Table[64] ,
		unsigned char Standard_Chromiance_Quantization_Table0[64] ,
		unsigned char Standard_Chromiance_Quantization_Table1[64] ,
		const unsigned char buffer[MAX_ROWBUF_WIDTH*3*8],

		pixel_queue_t& queue,bool flush

		)
{


	yuv_t yQ[64];
	yuv_t uQ[64];
	yuv_t vQ[64];
	freq_t yfrqQ[64];
	freq_t ufrqQ[64];
	freq_t vfrqQ[64];
	freq_t yquantQ[64];
	freq_t uquantQ[64];
	freq_t vquantQ[64];
	bits_t yBitQ[MAX_HUFF];
	bits_t uBitQ[MAX_HUFF];
	bits_t vBitQ[MAX_HUFF];
	bits_t emptyBitQ[1];

	bitlen_t yBitLenQ[MAX_HUFF];
	bitlen_t uBitLenQ[MAX_HUFF];
	bitlen_t vBitLenQ[MAX_HUFF];
	bitlen_t emptyBitLenQ[1];
	unsigned char yLen, uLen, vLen, emptyLen;
	emptyLen=1;
	bool ydcten, udcten, vdcten, yqen, vqen, uqen, yhen, uhen, vhen;
	colorBuffer( color_mode, sampling, imagewidth, buffer, yQ, uQ, vQ, ydcten, udcten, vdcten);
	dct(ydcten, yQ,yfrqQ, yqen);
	dct(udcten, uQ,ufrqQ, vqen);
	dct(vdcten, vQ,vfrqQ, uqen);
	quant_y(yqen, Standard_Luminance_Quantization_Table, yfrqQ, yquantQ  , yhen);
	quant_u(vqen, Standard_Chromiance_Quantization_Table0, ufrqQ, uquantQ, uhen);
	quant_v(uqen, Standard_Chromiance_Quantization_Table1, vfrqQ, vquantQ, vhen);

	huffman_Y (yhen, yquantQ, yBitQ, yBitLenQ, yLen);
	huffman_UV(uhen, uquantQ, uBitQ, uBitLenQ,U_DATA,uLen);
	huffman_UV(vhen, vquantQ, vBitQ, vBitLenQ,V_DATA,vLen);

	bitWriter(yBitQ, yBitLenQ,yLen,false, queue);
	bitWriter(uBitQ, uBitLenQ,uLen,false, queue);
	bitWriter(vBitQ, vBitLenQ,vLen,false, queue);
	bitWriter(emptyBitQ, emptyBitLenQ,emptyLen,false, queue);
}
void *analyzer()
{
    clock_t t1, t2;
    //short ana_arr[540] = {0};
    float act_time,msec;
    unsigned char enc_speech[11];
    unsigned char array1[176] = {0x00};
    int b = 0, loc = 0, count_64blocks = 0;
    int i = 0;

    while(count != 0)
    {
        sem_wait(&id_analyzer);
        //sem_wait(&mutx);
        //if (count_64blocks == 0)

        t1 = clock();

        memcpy(ana_arr, buff, 1080);

        sem_post(&empty);

        //t1 = clock();
        num_frames = (int16_t) ((rate == RATE2400) ? 1 : NF);

        //for (i = 0; i < num_frames; i++)
        //{
        //	dc_rmv(&ana_arr[i * FRAME], &hpspeech[IN_BEG + i * FRAME],
        //			dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        //	//melp_ana(&hpspeech[i * FRAME], &melp_par[i], i);
        //}

        dc_rmv(&ana_arr[0], &hpspeech[IN_BEG + 0],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[0], &melp_par[0],0);

        dc_rmv(&ana_arr[FRAME], &hpspeech[IN_BEG + FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[FRAME], &melp_par[1], 1);

        dc_rmv(&ana_arr[2 * FRAME], &hpspeech[IN_BEG + 2 * FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[2 * FRAME], &melp_par[2], 2);

        /*sem_post(&start_melp_1ana);
        sem_post(&start_melp_2ana);
        sem_post(&start_melp_3ana);


        sem_wait(&melp_1ana);
        sem_wait(&melp_2ana);
        sem_wait(&melp_3ana);*/


        sc_ana(melp_par);

        /* ======== Quantization ======== */

        lpc[0] = ONE_Q12;

        lsf_vq(melp_par);

        pitch_vq(melp_par);

        gain_vq(melp_par);

        quant_u(&melp_par[0].jitter, &(quant_par.jit_index[0]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[1].jitter, &(quant_par.jit_index[1]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[2].jitter, &(quant_par.jit_index[2]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        //for (i = 0; i < NF; i++)
        //	quant_u(&melp_par[i].jitter, &(quant_par.jit_index[i]), 0,
        //		MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        /* Quantize bandpass voicing */

        quant_bp(melp_par, num_frames);

        quant_jitter(melp_par);

        /* Calculate Fourier coeffs of residual from quantized LPC */
        //for (i = 0; i < num_frames; i++)
        //{
        //	/* The following fill() action is believed to be unnecessary. */
        //	//fill(melp_par[i].fs_mag, ONE_Q13, NUM_HARM);
        //
        //	if (!melp_par[i].uv_flag)
        //	{
        //		lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
        //		zerflt(&hpspeech
        //			   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
        //			   sigbuf, LPC_ORD, LPC_FRAME);
        //
        //		window(sigbuf, win_cof, sigbuf, LPC_FRAME);
        //
        //		find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
        //			  LPC_FRAME);
        //	}
        //}
        i = 0;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 1;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 2;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }

        quant_fsmag(melp_par);

        //for (i = 0; i < num_frames; i++)
        //	quant_par.uv_flag[i] = melp_par[i].uv_flag;
        quant_par.uv_flag[0] = melp_par[0].uv_flag;
        quant_par.uv_flag[1] = melp_par[1].uv_flag;
        quant_par.uv_flag[2] = melp_par[2].uv_flag;

        /* Write channel bitstream */

#if !SKIP_CHANNEL
        low_rate_chn_write(&quant_par);
#endif

        /* Update delay buffers for next block */
        v_equ(hpspeech, &(hpspeech[num_frames * FRAME]), IN_BEG);

        memcpy(enc_speech,chbuf,11);



        /*======================old content of melp thread======================*/
        //melpe_a(enc_speech, buff);
        //fwrite(enc_speech,sizeof(char),11,fptr);
        //=======================================================================
        // ather_jawad work for 2400 baudrate
        //=======================================================================
        for (b=0; b<11; b++)
        {
            array1[loc] = enc_speech[b];
            loc++;
        }

        count_64blocks = count_64blocks + 1;
        if(count_64blocks == 16)
        {
            loc = 0;
            memcpy(array2,array1,176);
            sem_post(&last);

            count_64blocks = 0;
        }

        t2 = clock() - t1;
        //printf("Time %d = %f\n\n",count_64blocks,(float)t2/CLOCKS_PER_SEC);

        //sem_post(&full);
    }
    pthread_exit(NULL);
}
Exemplo n.º 3
0
/****************************************************************************
**
** Function:		pitch_vq
**
** Description: 	Pitch values of three frames are vector quantized
**
** Arguments:
**
**	melp_param *par ---- input/output melp parameters
**
** Return value:	None
**
*****************************************************************************/
void pitch_vq(struct melp_param *par)
{
    register int16_t i;
    static BOOLEAN prev_uv_flag = TRUE;
    static int16_t prev_pitch = LOG_UV_PITCH_Q12;	/* Q12 */
    static int16_t prev_qpitch = LOG_UV_PITCH_Q12;	/* Q12 */
    const int16_t *codebook;
    int16_t cnt, size, pitch_index;
    int16_t temp1, temp2;
    int32_t L_temp;
    int16_t dcb[PITCH_VQ_CAND * NF];	/* Q12 */
    int16_t target[NF], deltp[NF];	/* Q12 */
    int16_t deltw[NF];	/* Q0 */
    int16_t weights[NF];	/* Q0 */
    int16_t indexlist[PITCH_VQ_CAND];
    int32_t distlist[PITCH_VQ_CAND];	/* Q25 */

    /* ---- Compute pitch in log domain ---- */
    for (i = 0; i < NF; i++)
        target[i] = log10_fxp(par[i].pitch, 7);	/* Q12 */

    cnt = 0;
    for (i = 0; i < NF; i++) {
        if (par[i].uv_flag)
            weights[i] = 0;	/* Q0 */
        else {
            weights[i] = 1;
            cnt++;
        }
    }

    /* ---- calculate delta ---- */
    for (i = 0; i < NF; i++) {
        if (prev_uv_flag || par[i].uv_flag) {
            deltp[i] = 0;
            deltw[i] = 0;
        } else {
            deltp[i] = melpe_sub(target[i], prev_pitch);
            deltw[i] = DELTA_PITCH_WEIGHT_Q0;
        }
        prev_pitch = target[i];
        prev_uv_flag = par[i].uv_flag;
    }

    if (cnt == 0) {

        for (i = 0; i < NF; i++)
            par[i].pitch = UV_PITCH;
        prev_qpitch = LOG_UV_PITCH_Q12;

    } else if (cnt == 1) {

        for (i = 0; i < NF; i++) {
            if (!par[i].uv_flag) {
                quant_u(&target[i], &(quant_par.pitch_index),
                        PIT_QLO_Q12, PIT_QUP_Q12, PIT_QLEV_M1,
                        PIT_QLEV_M1_Q8, TRUE, 7);
                quant_u_dec(quant_par.pitch_index,
                            &par[i].pitch, PIT_QLO_Q12,
                            PIT_QUP_Q12, PIT_QLEV_M1_Q8, 7);
            } else
                par[i].pitch = LOG_UV_PITCH_Q12;
        }

        /* At this point par[].pitch temporarily holds the pitches in the     */
        /* log domain with Q12.                                               */

        prev_qpitch = par[NF - 1].pitch;	/* Q12 */

        for (i = 0; i < NF; i++)
            par[i].pitch = pow10_fxp(par[i].pitch, 7);	/* Q7 */

    } else if (cnt > 1) {	/* cnt == 2, 3, ......, (NF - 1) */
        /* ----- set pointer ----- */
        if (cnt == NF) {	/* All NF frames are voiced. */
            codebook = pitch_vq_cb_vvv;
            size = PITCH_VQ_LEVEL_VVV;
        } else {
            codebook = pitch_vq_cb_uvv;
            size = PITCH_VQ_LEVEL_UVV;
        }		/* This part changed !!! (12/13/99) */

        /* ---- select candidate using static pitch distortion ---- */
        wvq1(target, weights, codebook, NF, size, indexlist, distlist,
             PITCH_VQ_CAND);

        /* -- select index using static and delta pitch distortion -- */
        temp1 = 0;
        for (i = 0; i < PITCH_VQ_CAND; i++) {
            L_temp = melpe_L_mult(indexlist[i], NF);
            L_temp = melpe_L_shr(L_temp, 1);
            temp2 = melpe_extract_l(L_temp);

            /* Now temp1 is (i*NF) and temp2 is (indexlist[i]*NF).            */

            dcb[temp1] = melpe_sub(codebook[temp2], prev_qpitch);	/* Q12 */
            v_equ(&dcb[temp1 + 1], &codebook[temp2 + 1], NF - 1);
            v_sub(&dcb[temp1 + 1], &codebook[temp2], NF - 1);
            temp1 = melpe_add(temp1, NF);
        }

        pitch_index = wvq2(deltp, deltw, dcb, NF, indexlist, distlist,
                           PITCH_VQ_CAND);

        if (par[NF - 1].uv_flag)
            prev_qpitch = LOG_UV_PITCH_Q12;
        else
            prev_qpitch = codebook[pitch_index * NF + NF - 1];	/* Q12 */

        for (i = 0; i < NF; i++) {
            if (par[i].uv_flag)
                par[i].pitch = UV_PITCH_Q7;
            else
                par[i].pitch =
                    pow10_fxp(codebook[pitch_index * NF + i],
                              7);
        }

        quant_par.pitch_index = pitch_index;
    }
}
Exemplo n.º 4
0
void melp_ana(float sp_in[],struct melp_param *par)
{

    int i;
    int begin;
    float sub_pitch;
    float temp,pcorr,bpthresh;
    float r[LPC_ORD+1],refc[LPC_ORD+1],lpc[LPC_ORD+1];
    float weights[LPC_ORD];
        
    /* Remove DC from input speech */
    dc_rmv(sp_in,&speech[IN_BEG],dcdel,FRAME);
    
    /* Copy input speech to pitch window and lowpass filter */
    v_equ(&sigbuf[LPF_ORD],&speech[PITCH_BEG],PITCH_FR);
    v_equ(sigbuf,lpfsp_del,LPF_ORD);
    polflt(&sigbuf[LPF_ORD],lpf_den,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    v_equ(lpfsp_del,&sigbuf[FRAME],LPF_ORD);
    zerflt(&sigbuf[LPF_ORD],lpf_num,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    
    /* Perform global pitch search at frame end on lowpass speech signal */
    /* Note: avoid short pitches due to formant tracking */
    fpitch[END] = find_pitch(&sigbuf[LPF_ORD+(PITCH_FR/2)],&temp,
			     (2*PITCHMIN),PITCHMAX,PITCHMAX);
    
    /* Perform bandpass voicing analysis for end of frame */
    bpvc_ana(&speech[FRAME_END], fpitch, &par->bpvc[0], &sub_pitch);
    
    /* Force jitter if lowest band voicing strength is weak */    
    if (par->bpvc[0] < VJIT)
	par->jitter = MAX_JITTER;
    else
	par->jitter = 0.0;
    
    /* Calculate LPC for end of frame */
    window(&speech[(FRAME_END-(LPC_FRAME/2))],win_cof,sigbuf,LPC_FRAME);
    autocorr(sigbuf,r,LPC_ORD,LPC_FRAME);
    lpc[0] = 1.0;
    lpc_schur(r,lpc,refc,LPC_ORD);
    lpc_bw_expand(lpc,lpc,BWFACT,LPC_ORD);
    
    /* Calculate LPC residual */
    zerflt(&speech[PITCH_BEG],lpc,&sigbuf[LPF_ORD],LPC_ORD,PITCH_FR);
        
    /* Check peakiness of residual signal */
    begin = (LPF_ORD+(PITCHMAX/2));
    temp = peakiness(&sigbuf[begin],PITCHMAX);
    
    /* Peakiness: force lowest band to be voiced  */
    if (temp > PEAK_THRESH) {
	par->bpvc[0] = 1.0;
    }
    
    /* Extreme peakiness: force second and third bands to be voiced */
    if (temp > PEAK_THR2) {
	par->bpvc[1] = 1.0;
	par->bpvc[2] = 1.0;
    }
		
    /* Calculate overall frame pitch using lowpass filtered residual */
    par->pitch = pitch_ana(&speech[FRAME_END], &sigbuf[LPF_ORD+PITCHMAX], 
			   sub_pitch,pitch_avg,&pcorr);
    bpthresh = BPTHRESH;
    
    /* Calculate gain of input speech for each gain subframe */
    for (i = 0; i < NUM_GAINFR; i++) {
	if (par->bpvc[0] > bpthresh) {

	    /* voiced mode: pitch synchronous window length */
	    temp = sub_pitch;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,MIN_GAINFR,2*PITCHMAX);
	}
	else {
	    temp = 1.33*GAINFR - 0.5;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,0,2*PITCHMAX);
	}
    }
    
    /* Update average pitch value */
    if (par->gain[NUM_GAINFR-1] > SILENCE_DB)
      temp = pcorr;
    else
      temp = 0.0;
    pitch_avg = p_avg_update(par->pitch, temp, VMIN);
    
    /* Calculate Line Spectral Frequencies */
    lpc_pred2lsp(lpc,par->lsf,LPC_ORD);
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize MELP parameters to 2400 bps and generate bitstream */
    
    /* Quantize LSF's with MSVQ */
    vq_lspw(weights, &par->lsf[1], lpc, LPC_ORD);
    msvq_enc(&par->lsf[1], weights, &par->lsf[1], vq_par);
    par->msvq_index = vq_par.indices;
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize logarithmic pitch period */
    /* Reserve all zero code for completely unvoiced */
    par->pitch = log10(par->pitch);
    quant_u(&par->pitch,&par->pitch_index,PIT_QLO,PIT_QUP,PIT_QLEV);
    par->pitch = pow(10.0,par->pitch);
    
    /* Quantize gain terms with uniform log quantizer	*/
    q_gain(par->gain, par->gain_index,GN_QLO,GN_QUP,GN_QLEV);
    
    /* Quantize jitter and bandpass voicing */
    quant_u(&par->jitter,&par->jit_index,0.0,MAX_JITTER,2);
    par->uv_flag = q_bpvc(&par->bpvc[0],&par->bpvc_index,bpthresh,
			  NUM_BANDS);
    
    /*	Calculate Fourier coefficients of residual signal from quantized LPC */
    fill(par->fs_mag,1.0,NUM_HARM);
    if (par->bpvc[0] > bpthresh) {
	lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
	zerflt(&speech[(FRAME_END-(LPC_FRAME/2))],lpc,sigbuf,
	       LPC_ORD,LPC_FRAME);
	window(sigbuf,win_cof,sigbuf,LPC_FRAME);
	find_harm(sigbuf, par->fs_mag, par->pitch, NUM_HARM, LPC_FRAME);
    }
    
    /* quantize Fourier coefficients */
    /* pre-weight vector, then use Euclidean distance */
    window(&par->fs_mag[0],w_fs,&par->fs_mag[0],NUM_HARM);
    fsvq_enc(&par->fs_mag[0], &par->fs_mag[0], fs_vq_par);
    
    /* Set MELP indeces to point to same array */
    par->fsvq_index = fs_vq_par.indices;

    /* Update MSVQ information */
    par->msvq_stages = vq_par.num_stages;
    par->msvq_bits = vq_par.num_bits;

    /* Write channel bitstream */
    melp_chn_write(par);

    /* Update delay buffers for next frame */
    v_equ(&speech[0],&speech[FRAME],IN_BEG);
    fpitch[BEGIN] = fpitch[END];
}