Ignore:
Timestamp:
Aug 13, 2008 6:21:03 PM (16 years ago)
Author:
bennylp
Message:

More ticket #590: optimizations in the echo suppressor, gained more than 3x speed. Added entries in mips_test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia/echo_suppress.c

    r2208 r2212  
    244244    float        best_corr;         /* Best correlation so far.             */ 
    245245 
     246    unsigned     sum_rec_level;     /* Running sum of level in rec_hist     */ 
     247    float        rec_corr;          /* Running corr in rec_hist.            */ 
     248 
     249    unsigned     sum_play_level0;   /* Running sum of level for first pos   */ 
     250    float        play_corr0;        /* Running corr for first pos .         */ 
     251 
    246252    float       *min_factor;        /* Array of minimum scaling factor      */ 
    247253    float       *avg_factor;        /* Array of average scaling factor      */ 
     
    354360    ec->residue = 0; 
    355361    ec->running_cnt = 0; 
     362    ec->sum_rec_level = ec->sum_play_level0 = 0; 
     363    ec->rec_corr = ec->play_corr0 = 0; 
    356364} 
    357365 
     
    376384    ec->residue = 0; 
    377385    ec->running_cnt = 0; 
     386    ec->sum_rec_level = ec->sum_play_level0 = 0; 
     387    ec->rec_corr = ec->play_corr0 = 0; 
    378388 
    379389    PJ_LOG(4,(THIS_FILE, "Echo suppressor soft reset. Re-learning..")); 
     
    404414{ 
    405415    int prev_index; 
    406     unsigned i, frm_level, sum_rec_level; 
    407     float rec_corr; 
     416    unsigned i, j, frm_level, sum_play_level, ulaw; 
     417    pj_uint16_t old_rec_frm_level, old_play_frm_level; 
     418    float play_corr; 
    408419 
    409420    ++ec->update_cnt; 
     
    415426    ++frm_level; /* to avoid division by zero */ 
    416427 
     428    /* Save the oldest frame level for later */ 
     429    old_play_frm_level = ec->play_hist[0]; 
     430 
    417431    /* Push current frame level to the back of the play history */ 
    418432    pj_array_erase(ec->play_hist, sizeof(pj_uint16_t), ec->play_hist_cnt, 0); 
     
    423437    ++frm_level; /* to avoid division by zero */ 
    424438 
     439    /* Save the oldest frame level for later */ 
     440    old_rec_frm_level = ec->rec_hist[0]; 
     441 
    425442    /* Push to the back of the rec history */ 
    426443    pj_array_erase(ec->rec_hist, sizeof(pj_uint16_t), ec->templ_cnt, 0); 
     
    438455 
    439456    /* Calculate rec signal pattern */ 
    440     rec_corr = 0; 
    441     sum_rec_level = 0; 
    442     for (i=0; i < ec->templ_cnt-1; ++i) { 
    443         float corr; 
    444         corr = (float)ec->rec_hist[i+1] / ec->rec_hist[i]; 
    445         rec_corr += corr; 
    446         sum_rec_level += ec->rec_hist[i]; 
    447     } 
    448     sum_rec_level += ec->rec_hist[i]; 
     457    if (ec->sum_rec_level == 0) { 
     458        /* Buffer has just been filled up, do full calculation */ 
     459        ec->rec_corr = 0; 
     460        ec->sum_rec_level = 0; 
     461        for (i=0; i < ec->templ_cnt-1; ++i) { 
     462            float corr; 
     463            corr = (float)ec->rec_hist[i+1] / ec->rec_hist[i]; 
     464            ec->rec_corr += corr; 
     465            ec->sum_rec_level += ec->rec_hist[i]; 
     466        } 
     467        ec->sum_rec_level += ec->rec_hist[i]; 
     468    } else { 
     469        /* Update from previous calculation */ 
     470        ec->sum_rec_level = ec->sum_rec_level - old_rec_frm_level +  
     471                            ec->rec_hist[ec->templ_cnt-1]; 
     472        ec->rec_corr = ec->rec_corr - ((float)ec->rec_hist[0] /  
     473                                              old_rec_frm_level) + 
     474                       ((float)ec->rec_hist[ec->templ_cnt-1] / 
     475                               ec->rec_hist[ec->templ_cnt-2]); 
     476    } 
    449477 
    450478    /* Iterate through the play history and calculate the signal correlation 
     
    453481     * to detect echo. 
    454482     */ 
    455     for (i=0; i < ec->tail_cnt; ++i) { 
    456         unsigned j, end, sum_play_level, ulaw; 
    457         float play_corr = 0, corr_diff; 
    458  
     483    /*  
     484     * First phase: do full calculation for the first position  
     485     */ 
     486    if (ec->sum_play_level0 == 0) { 
     487        /* Buffer has just been filled up, do full calculation */ 
    459488        sum_play_level = 0; 
    460         for (j=i, end=i+ec->templ_cnt-1; j<end; ++j) { 
     489        play_corr = 0; 
     490        for (j=0; j<ec->templ_cnt-1; ++j) { 
    461491            float corr; 
    462492            corr = (float)ec->play_hist[j+1] / ec->play_hist[j]; 
     
    465495        } 
    466496        sum_play_level += ec->play_hist[j]; 
     497        ec->sum_play_level0 = sum_play_level; 
     498        ec->play_corr0 = play_corr; 
     499    } else { 
     500        /* Update from previous calculation */ 
     501        ec->sum_play_level0 = ec->sum_play_level0 - old_play_frm_level +  
     502                              ec->play_hist[ec->templ_cnt-1]; 
     503        ec->play_corr0 = ec->play_corr0 - ((float)ec->play_hist[0] /  
     504                                                  old_play_frm_level) + 
     505                         ((float)ec->play_hist[ec->templ_cnt-1] / 
     506                                 ec->play_hist[ec->templ_cnt-2]); 
     507        sum_play_level = ec->sum_play_level0; 
     508        play_corr = ec->play_corr0; 
     509    } 
     510    ec->tmp_corr[0] = FABS(play_corr - ec->rec_corr); 
     511    ec->tmp_factor[0] = (float)ec->sum_rec_level / sum_play_level; 
     512 
     513    /* Bail out if remote isn't talking */ 
     514    ulaw = pjmedia_linear2ulaw(sum_play_level/ec->templ_cnt) ^ 0xFF; 
     515    if (ulaw < MIN_SIGNAL_ULAW) { 
     516        echo_supp_set_state(ec, ST_REM_SILENT, ulaw); 
     517        return; 
     518    } 
     519    /* Bail out if local user is talking */ 
     520    if (ec->sum_rec_level > sum_play_level) { 
     521        echo_supp_set_state(ec, ST_LOCAL_TALK, ulaw); 
     522        return; 
     523    } 
     524 
     525    /* 
     526     * Second phase: do incremental calculation for the rest of positions 
     527     */ 
     528    for (i=1; i < ec->tail_cnt; ++i) { 
     529        unsigned end; 
     530 
     531        end = i + ec->templ_cnt; 
     532 
     533        sum_play_level = sum_play_level - ec->play_hist[i-1] + 
     534                         ec->play_hist[end-1]; 
     535        play_corr = play_corr - ((float)ec->play_hist[i]/ec->play_hist[i-1]) + 
     536                    ((float)ec->play_hist[end-1]/ec->play_hist[end-2]); 
    467537 
    468538        /* Bail out if remote isn't talking */ 
     
    474544 
    475545        /* Bail out if local user is talking */ 
    476         if (sum_rec_level >= sum_play_level) { 
     546        if (ec->sum_rec_level > sum_play_level) { 
    477547            echo_supp_set_state(ec, ST_LOCAL_TALK, ulaw); 
    478548            return; 
     
    482552        // disabled: not a good idea if mic throws out loud echo 
    483553        /* Also bail out if we suspect there's a doubletalk */ 
    484         ulaw = pjmedia_linear2ulaw(sum_rec_level/ec->templ_cnt) ^ 0xFF; 
     554        ulaw = pjmedia_linear2ulaw(ec->sum_rec_level/ec->templ_cnt) ^ 0xFF; 
    485555        if (ulaw > MIN_SIGNAL_ULAW) { 
    486556            echo_supp_set_state(ec, ST_DOUBLETALK, ulaw); 
     
    490560 
    491561        /* Calculate correlation and save to temporary array */ 
    492         corr_diff = FABS(play_corr - rec_corr); 
    493         ec->tmp_corr[i] = corr_diff; 
     562        ec->tmp_corr[i] = FABS(play_corr - ec->rec_corr); 
    494563 
    495564        /* Also calculate the gain factor between mic and speaker level */ 
    496         ec->tmp_factor[i] = (float)sum_rec_level / sum_play_level; 
     565        ec->tmp_factor[i] = (float)ec->sum_rec_level / sum_play_level; 
    497566        pj_assert(ec->tmp_factor[i] < 1); 
    498567    } 
     
    653722                factor = 1.0; 
    654723                echo_supp_set_state(ec, ST_LOCAL_TALK, rec_level); 
    655             } else if (rec_level >= play_level) { 
     724            } else if (rec_level > play_level) { 
    656725                /* Seems that both are talking. Scale the mic signal 
    657726                 * down a little bit to reduce echo, while allowing both 
     
    689758            factor = (factor + ec->last_factor) / 2; 
    690759        else 
    691             factor = (factor + ec->last_factor*9) / 10; 
     760            factor = (factor + ec->last_factor*19) / 20; 
    692761 
    693762        /* Amplify frame */ 
Note: See TracChangeset for help on using the changeset viewer.