TCH AMR codec in GSM

bob wisebob avwiseav at gmail.com
Fri Jul 6 09:30:49 UTC 2012


 Hi List!

I am studying the AMR codec in GSM.but I met some problem.

I use burst_id to capture TCH in my test network,and in my programmm,
I use  the firsrt 8 bits of a TCH block to decide whether it is a tch or
not
and also get the codec mode of a AFS,and than other operations,but it is
not to work , I am struggled to this, any one can help me?
this is my code:

static void process_tch_amr(struct osmocom_ms *ms,struct l1ctl_burst_ind
*bi)
{
    int16_t rx_dbm;
    uint16_t arfcn;
    uint32_t fn,B;
    uint8_t cbits, tn, lch_idx;
    int ul, bid, i, k, length;
    sbit_t *bursts, mC[456];
    ubit_t steal_bit[2],
bt[114],convu[260],convd[260],EFRBits[244],EFRAMR[8 + 244];

    pbit_t voice[33];
    unsigned char mode;

    arfcn  = ntohs(bi->band_arfcn);
    rx_dbm = rxlev2dbm(bi->rx_level);

    fn     = ntohl(bi->frame_nr);
    ul     = !!(arfcn & ARFCN_UPLINK);

    cbits  = bi->chan_nr >> 3;
    tn     = bi->chan_nr & 7;

    B  = -1;

    if((fn%13)%4 == 0) flag= 1;

    if(!flag) return;

    B = count_tch % 8;

    if (B == -1)
        return;


    if (B == 0){
        for(i=0; i<4; i++){
            memset(app_state.mI[i], 0x00, 114);
        }
    }else if(B == 4){
        for(i=4; i<8; i++){
            memset(app_state.mI[i], 0x00, 114);
        }
    }

    /* Unpack (ignore hu/hl) */
    osmo_pbit2ubit_ext(bt,  0, bi->bits,  0, 57, 0);
    osmo_pbit2ubit_ext(bt, 57, bi->bits, 57, 57, 0);

    /* Convert to softbits */
    for (i=0; i<114; i++)
        app_state.mI[B][i] = bt[i] ? - (bi->snr >> 1) : (bi->snr >> 1);

    count_tch++;

    // Deinterleaves i[] to c[]
    if((B % 4 ==3)&&(count_tch >= 7)){
        if(B == 3)
        {
            tch_deinterleave(mC, 4);
        }else{
            tch_deinterleave(mC, 0);
        }

        //abstract the    codec mode
        for(i = 0;i<8;i++)
        {
            //printf("%x ",mC[i]);
            if(mC[i] < 0)
                mode = ( mode | (1 << i));
            else
                mode = ( mode & (~(1 << i)));
        }
        //printf("mode %x\n",mode);

        switch(mode){
            case CODEC_MODE_1:
                printf("codec 1\n");
                i = find_speech_mode(app_state.amr_arg.amr_acs, 1);
            break;
            case CODEC_MODE_2:
                printf("codec 2\n");
                i = find_speech_mode(app_state.amr_arg.amr_acs, 2);
            break;
            case CODEC_MODE_3:
                printf("codec 3\n");
                i = find_speech_mode(app_state.amr_arg.amr_acs, 3);
            break;
            case CODEC_MODE_4:
                printf("codec 4\n");
                i = find_speech_mode(app_state.amr_arg.amr_acs, 4);
            break;
            default:
            {
                //printf("unkown codec\n");
                return;
            }
        }

        memset(voice,0,32);
        switch( 1 << i ){
            case gsm690_4_75:
                printf("codec gsm690_4_75\n");
                osmo_conv_decode(&conv_tch_afs_4_75, &mC[8], convu);
                memcpy(convd,convu,39);
                memcpy(convd,&convu[45],56);
                tch_map(gsm690_4_75_bitorder, gsm690_4_75_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x04, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_4_75_len, 1);

            break;
            case gsm690_5_15:
                printf("codec gsm690_5_15\n");
                osmo_conv_decode(&conv_tch_afs_5_15, &mC[8], convu);
                memcpy(convd,convu,49);
                memcpy(convd,&convu[55],54);
                tch_map(gsm690_5_15_bitorder, gsm690_5_15_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x0c, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_5_15_len, 1);

            break;
            case gsm690_5_90:
                printf("codec gsm690_5_90\n");
                osmo_conv_decode(&conv_tch_afs_5_9, &mC[8], convu);
                memcpy(convd,convu,55);
                memcpy(convd,&convu[61],63);
                tch_map(gsm690_5_9_bitorder, gsm690_5_90_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x14, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_5_90_len, 1);

            break;
            case gsm690_6_70:
                printf("codec gsm690_6_70\n");
                osmo_conv_decode(&conv_tch_afs_6_7, &mC[8], convu);
                memcpy(convd,convu,55);
                memcpy(convd,&convu[61],79);
                tch_map(gsm690_6_7_bitorder, gsm690_6_70_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x1c, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_6_70_len, 1);

            break;
            case gsm690_7_40:
                printf("codec gsm690_7_40\n");
                osmo_conv_decode(&conv_tch_afs_7_4, &mC[8], convu);
                memcpy(convd,convu,61);
                memcpy(convd,&convu[67],87);
                tch_map(gsm690_7_4_bitorder, gsm690_7_40_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x24, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_7_40_len, 1);

            break;
            case gsm690_7_95:
                printf("codec gsm690_7_95\n");
                osmo_conv_decode(&conv_tch_afs_7_95, &mC[8], convu);
                memcpy(convd,convu,75);
                memcpy(convd,&convu[81],84);
                tch_map(gsm690_7_95_bitorder, gsm690_7_95_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x2c, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_7_95_len, 1);
            break;
            case gsm690_10_2:
                printf("codec gsm690_10_2\n");
                osmo_conv_decode(&conv_tch_afs_10_2, &mC[8], convu);
                memcpy(convd,convu,65);
                memcpy(convd,&convu[71],139);
                tch_map(gsm690_10_2_bitorder, gsm690_10_2_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x34, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_10_2_len, 1);
            break;
            case gsm690_12_2:
                printf("codec gsm690_12_2\n");
                osmo_conv_decode(&conv_tch_afs_12_2, &mC[8], convu);
                memcpy(convd,convu,81);
                memcpy(convd,&convu[87],163);
                tch_map(gsm690_12_2_bitorder, gsm690_12_2_len, EFRAMR +
8,convd);
                fillField(EFRAMR, 0, 0x3c, 8);
                length = osmo_ubit2pbit_ext(voice, 0, EFRAMR, 0, 8 +
gsm690_12_2_len, 1);
            break;
            default:
            {
                printf("unkown gsm690 codec\n");
                return;
            }
        }

        printf("length is %d\n",length);
        printf("voice[0] is %x\n",voice[0]);
        fwrite(voice, 1, length, d_speech_file);
     }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/baseband-devel/attachments/20120706/5e1dde36/attachment.html>


More information about the baseband-devel mailing list