Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <pjmedia/resample.h> |
| 21 | #include <pjmedia/errno.h> |
| 22 | #include <pj/assert.h> |
| 23 | #include <pj/log.h> |
| 24 | #include <pj/pool.h> |
| 25 | |
| 26 | #if PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX |
| 27 | |
| 28 | #include <speex/speex_resampler.h> |
| 29 | |
| 30 | #define THIS_FILE "resample_speex.c" |
| 31 | |
| 32 | |
| 33 | struct pjmedia_resample |
| 34 | { |
| 35 | SpeexResamplerState *state; |
| 36 | unsigned in_samples_per_frame; |
| 37 | unsigned out_samples_per_frame; |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool, |
| 42 | pj_bool_t high_quality, |
| 43 | pj_bool_t large_filter, |
| 44 | unsigned channel_count, |
| 45 | unsigned rate_in, |
| 46 | unsigned rate_out, |
| 47 | unsigned samples_per_frame, |
| 48 | pjmedia_resample **p_resample) |
| 49 | { |
| 50 | pjmedia_resample *resample; |
| 51 | int quality; |
| 52 | int err; |
| 53 | |
| 54 | PJ_ASSERT_RETURN(pool && p_resample && rate_in && |
| 55 | rate_out && samples_per_frame, PJ_EINVAL); |
| 56 | |
| 57 | resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample); |
| 58 | PJ_ASSERT_RETURN(resample, PJ_ENOMEM); |
| 59 | |
| 60 | if (high_quality) { |
| 61 | if (large_filter) |
| 62 | quality = 10; |
| 63 | else |
| 64 | quality = 7; |
| 65 | } else { |
| 66 | quality = 3; |
| 67 | } |
| 68 | |
| 69 | resample->in_samples_per_frame = samples_per_frame; |
| 70 | resample->out_samples_per_frame = rate_out / (rate_in / samples_per_frame); |
| 71 | resample->state = speex_resampler_init(channel_count, rate_in, rate_out, |
| 72 | quality, &err); |
| 73 | if (resample->state == NULL || err != RESAMPLER_ERR_SUCCESS) |
| 74 | return PJ_ENOMEM; |
| 75 | |
| 76 | |
| 77 | *p_resample = resample; |
| 78 | |
| 79 | PJ_LOG(5,(THIS_FILE, |
| 80 | "resample created: quality=%d, ch=%d, in/out rate=%d/%d", |
| 81 | quality, channel_count, rate_in, rate_out)); |
| 82 | return PJ_SUCCESS; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample, |
| 87 | const pj_int16_t *input, |
| 88 | pj_int16_t *output ) |
| 89 | { |
| 90 | spx_uint32_t in_length, out_length; |
| 91 | |
| 92 | PJ_ASSERT_ON_FAIL(resample, return); |
| 93 | |
| 94 | in_length = resample->in_samples_per_frame; |
| 95 | out_length = resample->out_samples_per_frame; |
| 96 | |
| 97 | speex_resampler_process_interleaved_int(resample->state, |
| 98 | (const __int16 *)input, &in_length, |
| 99 | (__int16 *)output, &out_length); |
| 100 | |
| 101 | pj_assert(in_length == resample->in_samples_per_frame); |
| 102 | pj_assert(out_length == resample->out_samples_per_frame); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample) |
| 107 | { |
| 108 | PJ_ASSERT_RETURN(resample != NULL, 0); |
| 109 | return resample->in_samples_per_frame; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample) |
| 114 | { |
| 115 | PJ_ASSERT_ON_FAIL(resample, return); |
| 116 | if (resample->state) { |
| 117 | speex_resampler_destroy(resample->state); |
| 118 | resample->state = NULL; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #else /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX */ |
| 123 | |
| 124 | int pjmedia_resample_speex_excluded; |
| 125 | |
| 126 | #endif /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_SPEEX */ |
| 127 | |