Index: pjproject/trunk/pjmedia/src/pjmedia/resample_resample.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia/resample_resample.c	(revision 2039)
+++ pjproject/trunk/pjmedia/src/pjmedia/resample_resample.c	(revision 2228)
@@ -41,5 +41,12 @@
     unsigned	 xoff;		/* History and lookahead size, in samples   */
     unsigned	 frame_size;	/* Samples per frame.			    */
+    unsigned	 channel_cnt;	/* Channel count.			    */
+
+    /* Buffer for monochannel */
     pj_int16_t	*buffer;	/* Input buffer.			    */
+
+    /* Buffer for multichannel */
+    pj_int16_t **in_buffer;	/* Array of input buffer for each channel.  */
+    pj_int16_t  *tmp_buffer;	/* Temporary output buffer for processing.  */
 };
 
@@ -59,8 +66,6 @@
 		     rate_out && samples_per_frame, PJ_EINVAL);
 
-    resample = PJ_POOL_ALLOC_T(pool, pjmedia_resample);
+    resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample);
     PJ_ASSERT_RETURN(resample, PJ_ENOMEM);
-
-    PJ_UNUSED_ARG(channel_count);
 
     /*
@@ -77,9 +82,8 @@
     resample->large_filter = large_filter;
     resample->high_quality = high_quality;
+    resample->channel_cnt = channel_count;
     resample->frame_size = samples_per_frame;
 
     if (high_quality) {
-	unsigned size;
-
 	/* This is a bug in xoff calculation, thanks Stephane Lussier
 	 * of Macadamian dot com.
@@ -87,5 +91,12 @@
 	 */
 	resample->xoff = res_GetXOFF(resample->factor, (char)large_filter);
-
+    } else {
+	resample->xoff = 1;
+    }
+
+    if (channel_count == 1) {
+	unsigned size;
+
+	/* Allocate input buffer */
 	size = (samples_per_frame + 2*resample->xoff) * sizeof(pj_int16_t);
 	resample->buffer = (pj_int16_t*) pj_pool_alloc(pool, size);
@@ -94,7 +105,25 @@
 	pjmedia_zero_samples(resample->buffer, resample->xoff*2);
 
-
-    } else {
-	resample->xoff = 0;
+    } else if (channel_count > 1) {
+	unsigned i, size;
+
+	/* Allocate input buffer table */
+	size = channel_count * sizeof(pj_int16_t*);
+	resample->in_buffer = (pj_int16_t**)pj_pool_alloc(pool, size);
+
+	/* Allocate input buffer */
+	size = (samples_per_frame/channel_count + 2*resample->xoff) * 
+	       sizeof(pj_int16_t);
+	for (i = 0; i < channel_count; ++i) {
+	    resample->in_buffer[i] = (pj_int16_t*)pj_pool_alloc(pool, size);
+	    PJ_ASSERT_RETURN(resample->in_buffer, PJ_ENOMEM);
+	    pjmedia_zero_samples(resample->in_buffer[i], resample->xoff*2);
+	}
+
+	/* Allocate temporary output buffer */
+	size = (unsigned) (resample->frame_size * sizeof(pj_int16_t) * 
+			   resample->factor / channel_count);
+	resample->tmp_buffer = (pj_int16_t*) pj_pool_alloc(pool, size);
+	PJ_ASSERT_RETURN(resample->tmp_buffer, PJ_ENOMEM);
     }
 
@@ -117,88 +146,139 @@
     PJ_ASSERT_ON_FAIL(resample, return);
 
-    if (resample->high_quality) {
+    /* Okay chaps, here's how we do resampling.
+     *
+     * The original resample algorithm requires xoff samples *before* the
+     * input buffer as history, and another xoff samples *after* the
+     * end of the input buffer as lookahead. Since application can only
+     * supply framesize buffer on each run, PJMEDIA needs to arrange the
+     * buffer to meet these requirements.
+     *
+     * So here comes the trick.
+     *
+     * First of all, because of the history and lookahead requirement, 
+     * resample->buffer need to accomodate framesize+2*xoff samples in its
+     * buffer. This is done when the buffer is created.
+     *
+     * On the first run, the input frame (supplied by application) is
+     * copied to resample->buffer at 2*xoff position. The first 2*xoff
+     * samples are initially zeroed (in the initialization). The resample
+     * algorithm then invoked at resample->buffer+xoff ONLY, thus giving
+     * it one xoff at the beginning as zero, and one xoff at the end
+     * as the end of the original input. The resample algorithm will see
+     * that the first xoff samples in the input as zero.
+     *
+     * So here's the layout of resample->buffer on the first run.
+     *
+     * run 0 
+     *     +------+------+--------------+
+     *     | 0000 | 0000 |  frame0...   |
+     *     +------+------+--------------+
+     *     ^      ^      ^              ^
+	 *     0    xoff  2*xoff       size+2*xoff 
+	 *
+     * (Note again: resample algorithm is called at resample->buffer+xoff)
+     *
+     * At the end of the run, 2*xoff samples from the end of 
+     * resample->buffer are copied to the beginning of resample->buffer.
+     * The first xoff part of this will be used as history for the next
+     * run, and the second xoff part of this is actually the start of
+     * resampling for the next run.
+     *
+     * And the first run completes, the function returns.
+     *
+     * 
+     * On the next run, the input frame supplied by application is again
+     * copied at 2*xoff position in the resample->buffer, and the 
+     * resample algorithm is again invoked at resample->buffer+xoff 
+     * position. So effectively, the resample algorithm will start its
+     * operation on the last xoff from the previous frame, and gets the
+     * history from the last 2*xoff of the previous frame, and the look-
+     * ahead from the last xoff of current frame.
+     *
+     * So on this run, the buffer layout is:
+     *
+     * run 1
+     *     +------+------+--------------+
+     *     | frm0 | frm0 |  frame1...   |
+     *     +------+------+--------------+
+     *     ^      ^      ^              ^
+	 *     0    xoff  2*xoff       size+2*xoff 
+     *
+     * As you can see from above diagram, the resampling algorithm is
+     * actually called from the last xoff part of previous frame (frm0).
+     *
+     * And so on the process continues for the next frame, and the next,
+     * and the next, ...
+     *
+     */
+    if (resample->channel_cnt == 1) {
 	pj_int16_t *dst_buf;
 	const pj_int16_t *src_buf;
 
-	/* Okay chaps, here's how we do resampling.
-	 *
-	 * The original resample algorithm requires xoff samples *before* the
-	 * input buffer as history, and another xoff samples *after* the
-	 * end of the input buffer as lookahead. Since application can only
-	 * supply framesize buffer on each run, PJMEDIA needs to arrange the
-	 * buffer to meet these requirements.
-	 *
-	 * So here comes the trick.
-	 *
-	 * First of all, because of the history and lookahead requirement, 
-	 * resample->buffer need to accomodate framesize+2*xoff samples in its
-	 * buffer. This is done when the buffer is created.
-	 *
-	 * On the first run, the input frame (supplied by application) is
-	 * copied to resample->buffer at 2*xoff position. The first 2*xoff
-	 * samples are initially zeroed (in the initialization). The resample
-	 * algorithm then invoked at resample->buffer+xoff ONLY, thus giving
-	 * it one xoff at the beginning as zero, and one xoff at the end
-	 * as the end of the original input. The resample algorithm will see
-	 * that the first xoff samples in the input as zero.
-	 *
-	 * So here's the layout of resample->buffer on the first run.
-	 *
-	 * run 0 
-	 *     +------+------+--------------+
-	 *     | 0000 | 0000 |  frame0...   |
-	 *     +------+------+--------------+
-	 *     ^      ^      ^              ^
-         *     0    xoff  2*xoff       size+2*xoff 
-         *
-	 * (Note again: resample algorithm is called at resample->buffer+xoff)
-	 *
-	 * At the end of the run, 2*xoff samples from the end of 
-	 * resample->buffer are copied to the beginning of resample->buffer.
-	 * The first xoff part of this will be used as history for the next
-	 * run, and the second xoff part of this is actually the start of
-	 * resampling for the next run.
-	 *
-	 * And the first run completes, the function returns.
-	 *
-	 * 
-	 * On the next run, the input frame supplied by application is again
-	 * copied at 2*xoff position in the resample->buffer, and the 
-	 * resample algorithm is again invoked at resample->buffer+xoff 
-	 * position. So effectively, the resample algorithm will start its
-	 * operation on the last xoff from the previous frame, and gets the
-	 * history from the last 2*xoff of the previous frame, and the look-
-	 * ahead from the last xoff of current frame.
-	 *
-	 * So on this run, the buffer layout is:
-	 *
-	 * run 1
-	 *     +------+------+--------------+
-	 *     | frm0 | frm0 |  frame1...   |
-	 *     +------+------+--------------+
-	 *     ^      ^      ^              ^
-         *     0    xoff  2*xoff       size+2*xoff 
-	 *
-	 * As you can see from above diagram, the resampling algorithm is
-	 * actually called from the last xoff part of previous frame (frm0).
-	 *
-	 * And so on the process continues for the next frame, and the next,
-	 * and the next, ...
-	 *
-	 */
+	/* Prepare input frame */
 	dst_buf = resample->buffer + resample->xoff*2;
 	pjmedia_copy_samples(dst_buf, input, resample->frame_size);
 
-	res_Resample(resample->buffer + resample->xoff, output,
-		     resample->factor, (pj_uint16_t)resample->frame_size,
-		     (char)resample->large_filter, (char)PJ_TRUE);
-
+	/* Resample */
+	if (resample->high_quality) {
+	    res_Resample(resample->buffer + resample->xoff, output,
+			 resample->factor, (pj_uint16_t)resample->frame_size,
+			 (char)resample->large_filter, (char)PJ_TRUE);
+	} else {
+	    res_SrcLinear(resample->buffer + resample->xoff, output, 
+			  resample->factor, (pj_uint16_t)resample->frame_size);
+	}
+
+	/* Update history */
 	dst_buf = resample->buffer;
 	src_buf = input + resample->frame_size - resample->xoff*2;
 	pjmedia_copy_samples(dst_buf, src_buf, resample->xoff * 2);
 
-    } else {
-	res_SrcLinear( input, output, resample->factor, 
-		       (pj_uint16_t)resample->frame_size);
+    } else { /* Multichannel */
+	unsigned i, j;
+
+	for (i = 0; i < resample->channel_cnt; ++i) {
+	    pj_int16_t *dst_buf;
+	    const pj_int16_t *src_buf;
+	    unsigned mono_frm_sz_in;
+	    unsigned mono_frm_sz_out;
+    	
+	    mono_frm_sz_in  = resample->frame_size / resample->channel_cnt;
+	    mono_frm_sz_out = (unsigned)(mono_frm_sz_in * resample->factor);
+
+	    /* Deinterleave input */
+	    dst_buf = resample->in_buffer[i] + resample->xoff*2;
+	    src_buf = input + i;
+	    for (j = 0; j < mono_frm_sz_in; ++j) {
+		*dst_buf++ = *src_buf;
+		src_buf += resample->channel_cnt;
+	    }
+
+	    /* Resample this channel */
+	    if (resample->high_quality) {
+		res_Resample(resample->in_buffer[i] + resample->xoff,
+			     resample->tmp_buffer, resample->factor,
+			     (pj_uint16_t)mono_frm_sz_in,
+			     (char)resample->large_filter, (char)PJ_TRUE);
+	    } else {
+		res_SrcLinear( resample->in_buffer[i],
+			       resample->tmp_buffer, 
+			       resample->factor, 
+			       (pj_uint16_t)mono_frm_sz_in);
+	    }
+
+	    /* Update history */
+	    dst_buf = resample->in_buffer[i];
+	    src_buf = resample->in_buffer[i] + mono_frm_sz_in;
+	    pjmedia_copy_samples(dst_buf, src_buf, resample->xoff * 2);
+
+	    /* Reinterleave output */
+	    dst_buf = output + i;
+	    src_buf = resample->tmp_buffer;
+	    for (j = 0; j < mono_frm_sz_out; ++j) {
+		*dst_buf = *src_buf++;
+		dst_buf += resample->channel_cnt;
+	    }
+	}
     }
 }
