Changeset 495 for pjproject


Ignore:
Timestamp:
Jun 8, 2006 2:08:58 PM (18 years ago)
Author:
bennylp
Message:

Fixed bug in xoff calculation in PJMEDIA resample

File:
1 edited

Legend:

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

    r358 r495  
    530530    resample->large_filter = large_filter; 
    531531    resample->high_quality = high_quality; 
    532     resample->xoff = large_filter ? 32 : 6; 
    533532    resample->frame_size = samples_per_frame; 
    534533 
     
    537536        unsigned i; 
    538537 
     538        /* This is a bug in xoff calculation, thanks Stephane Lussier 
     539         * of Macadamian dot com. 
     540         *   resample->xoff = large_filter ? 32 : 6; 
     541         */ 
     542        if (large_filter) 
     543            resample->xoff = (LARGE_FILTER_NMULT + 1) / 2.0  *   
     544                             MAX(1.0, 1.0/resample->factor); 
     545        else 
     546            resample->xoff = (SMALL_FILTER_NMULT + 1) / 2.0  *   
     547                             MAX(1.0, 1.0/resample->factor); 
     548 
     549 
    539550        size = (samples_per_frame + 2*resample->xoff) * sizeof(pj_int16_t); 
    540551        resample->buffer = pj_pool_alloc(pool, size); 
     
    544555            resample->buffer[i] = 0; 
    545556        } 
     557 
     558 
     559    } else { 
     560        resample->xoff = 0; 
    546561    } 
    547562 
     
    563578        const pj_int16_t *src_buf; 
    564579 
    565         /* Buffer layout: 
     580        /* Okay chaps, here's how we do resampling. 
     581         * 
     582         * The original resample algorithm requires xoff samples *before* the 
     583         * input buffer as history, and another xoff samples *after* the 
     584         * end of the input buffer as lookahead. Since application can only 
     585         * supply framesize buffer on each run, PJMEDIA needs to arrange the 
     586         * buffer to meet these requirements. 
     587         * 
     588         * So here comes the trick. 
     589         * 
     590         * First of all, because of the history and lookahead requirement,  
     591         * resample->buffer need to accomodate framesize+2*xoff samples in its 
     592         * buffer. This is done when the buffer is created. 
     593         * 
     594         * On the first run, the input frame (supplied by application) is 
     595         * copied to resample->buffer at 2*xoff position. The first 2*xoff 
     596         * samples are initially zeroed (in the initialization). The resample 
     597         * algorithm then invoked at resample->buffer+xoff ONLY, thus giving 
     598         * it one xoff at the beginning as zero, and one xoff at the end 
     599         * as the end of the original input. The resample algorithm will see 
     600         * that the first xoff samples in the input as zero. 
     601         * 
     602         * So here's the layout of resample->buffer on the first run. 
    566603         * 
    567604         * run 0  
     
    572609         *     0    xoff  2*xoff       size+2*xoff  
    573610         * 
    574          * run 01 
     611         * (Note again: resample algorithm is called at resample->buffer+xoff) 
     612         * 
     613         * At the end of the run, 2*xoff samples from the end of  
     614         * resample->buffer are copied to the beginning of resample->buffer. 
     615         * The first xoff part of this will be used as history for the next 
     616         * run, and the second xoff part of this is actually the start of 
     617         * resampling for the next run. 
     618         * 
     619         * And the first run completes, the function returns. 
     620         * 
     621         *  
     622         * On the next run, the input frame supplied by application is again 
     623         * copied at 2*xoff position in the resample->buffer, and the  
     624         * resample algorithm is again invoked at resample->buffer+xoff  
     625         * position. So effectively, the resample algorithm will start its 
     626         * operation on the last xoff from the previous frame, and gets the 
     627         * history from the last 2*xoff of the previous frame, and the look- 
     628         * ahead from the last xoff of current frame. 
     629         * 
     630         * So on this run, the buffer layout is: 
     631         * 
     632         * run 1 
    575633         *     +------+------+--------------+ 
    576634         *     | frm0 | frm0 |  frame1...   | 
     
    578636         *     ^      ^      ^              ^ 
    579637         *     0    xoff  2*xoff       size+2*xoff  
     638         * 
     639         * As you can see from above diagram, the resampling algorithm is 
     640         * actually called from the last xoff part of previous frame (frm0). 
     641         * 
     642         * And so on the process continues for the next frame, and the next, 
     643         * and the next, ... 
     644         * 
    580645         */ 
    581646        dst_buf = resample->buffer + resample->xoff*2; 
Note: See TracChangeset for help on using the changeset viewer.