From c520cb042eb8bd38adf58f4b99031467ad237690 Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Mon, 14 Jul 2008 18:55:01 +0000 Subject: Ticket #570: Fixed find_pitch() search range iteration; and fixed wsola_generate() in deciding when to just return a frame from existing extra samples and when to expand/generate samples. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2145 74dad513-b988-da41-8d7b-12977e46ad98 --- pjmedia/src/pjmedia/wsola.c | 72 ++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 37 deletions(-) (limited to 'pjmedia') diff --git a/pjmedia/src/pjmedia/wsola.c b/pjmedia/src/pjmedia/wsola.c index ef26e1a5..359c891d 100644 --- a/pjmedia/src/pjmedia/wsola.c +++ b/pjmedia/src/pjmedia/wsola.c @@ -161,7 +161,7 @@ static pj_int16_t *find_pitch(pj_int16_t *frm, pj_int16_t *beg, pj_int16_t *end, int abs_corr = 0; /* Do calculation on 8 samples at once */ - for (i = 0; ibuf); pjmedia_circ_buf_set_len(wsola->buf, wsola->hist_size + wsola->min_extra); + pjmedia_zero_samples(wsola->buf->start, wsola->buf->len); return PJ_SUCCESS; } @@ -574,6 +560,14 @@ static void expand(pjmedia_wsola *wsola, unsigned needed) overlapp_add_simple(wsola->merge_buf, wsola->hanning_size, templ, start); } else { + /* Check if pointers are in the valid range */ + CHECK_(templ >= wsola->buf->buf && + templ + wsola->hanning_size <= + wsola->buf->buf + wsola->buf->capacity); + CHECK_(start >= wsola->buf->buf && + start + wsola->hanning_size <= + wsola->buf->buf + wsola->buf->capacity); + overlapp_add(wsola->merge_buf, wsola->hanning_size, templ, start, wsola->hanning); } @@ -582,8 +576,10 @@ static void expand(pjmedia_wsola *wsola, unsigned needed) dist = templ - start; /* Not enough buffer to hold the result */ - if (reg1_len + dist > wsola->buf_size) + if (reg1_len + dist > wsola->buf_size) { + pj_assert(!"WSOLA buffer size may be to small!"); break; + } /* Copy the "tail" (excess frame) to the end */ pjmedia_move_samples(templ + wsola->hanning_size, @@ -671,7 +667,6 @@ PJ_DEF(pj_status_t) pjmedia_wsola_save( pjmedia_wsola *wsola, pj_status_t status; buf_len = pjmedia_circ_buf_get_len(wsola->buf); - CHECK_(buf_len >= (unsigned)(wsola->hist_size + wsola->min_extra)); /* Update vars */ wsola->expand_cnt = 0; @@ -686,8 +681,11 @@ PJ_DEF(pj_status_t) pjmedia_wsola_save( pjmedia_wsola *wsola, pjmedia_circ_buf_get_read_regions(wsola->buf, ®1, ®1_len, ®2, ®2_len); + CHECK_(pjmedia_circ_buf_get_len(wsola->buf) >= + (unsigned)(wsola->hist_size + (wsola->min_extra<<1))); + if (reg2_len == 0) { - ola_left = reg1; + ola_left = reg1 + reg1_len - wsola->min_extra; } else if (reg2_len >= wsola->min_extra) { ola_left = reg2 + reg2_len - wsola->min_extra; } else { @@ -725,25 +723,25 @@ PJ_DEF(pj_status_t) pjmedia_wsola_save( pjmedia_wsola *wsola, PJ_DEF(pj_status_t) pjmedia_wsola_generate( pjmedia_wsola *wsola, pj_int16_t frm[]) { - unsigned samples_len; + unsigned samples_len, samples_req; + + pj_status_t status = PJ_SUCCESS; - samples_len = pjmedia_circ_buf_get_len(wsola->buf) - wsola->hist_size; - wsola->ts.u64 += wsola->samples_per_frame; + CHECK_(pjmedia_circ_buf_get_len(wsola->buf) >= wsola->hist_size + + wsola->min_extra); - if (samples_len < (unsigned)wsola->samples_per_frame + - (unsigned)wsola->min_extra) - { - unsigned new_samples; + /* Calculate how many samples in the buffer */ + samples_len = pjmedia_circ_buf_get_len(wsola->buf) - wsola->hist_size; - /* Calculate how many samples are needed for a new frame */ - new_samples = wsola->samples_per_frame + wsola->min_extra - - samples_len; - if (wsola->expand_cnt == 0) - new_samples += wsola->min_extra; + /* Calculate how many samples are required to be available in the buffer */ + samples_req = wsola->samples_per_frame + (wsola->min_extra << 1); + + wsola->ts.u64 += wsola->samples_per_frame; + if (samples_len < samples_req) { /* Expand buffer */ - expand(wsola, new_samples); + expand(wsola, samples_req - samples_len); TRACE_((THIS_FILE, "Buf size after expanded = %d", pjmedia_circ_buf_get_len(wsola->buf))); wsola->expand_cnt++; -- cgit v1.2.3