~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjmedia/src/pjmedia/resample_libsamplerate.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: resample_libsamplerate.c 3553 2011-05-05 06:14:19Z nanang $ */
 
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
/*
 
27
 * HOW TO ACTIVATE LIBSAMPLERATE (a.k.a SRC/Secret Rabbit Code) AS
 
28
 * PJMEDIA'S SAMPLE RATE CONVERSION BACKEND
 
29
 *
 
30
 * See README.txt in third_party/samplerate directory.
 
31
 */
 
32
 
 
33
 
 
34
#if PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_LIBSAMPLERATE
 
35
 
 
36
#include "../../third_party/libsamplerate/src/samplerate.h"
 
37
 
 
38
#define THIS_FILE   "resample_libsamplerate.c"
 
39
 
 
40
#if defined(_MSC_VER)
 
41
#   ifdef _DEBUG
 
42
#       pragma comment( lib, "../../third_party/lib/libsamplerate-i386-win32-vc-debug.lib")
 
43
#   else
 
44
#       pragma comment( lib, "../../third_party/lib/libsamplerate-i386-win32-vc-release.lib")
 
45
#   endif
 
46
#endif
 
47
 
 
48
 
 
49
struct pjmedia_resample
 
50
{
 
51
    SRC_STATE   *state;
 
52
    unsigned     in_samples;
 
53
    unsigned     out_samples;
 
54
    float       *frame_in, *frame_out;
 
55
    unsigned     in_extra, out_extra;
 
56
    double       ratio;
 
57
};
 
58
 
 
59
 
 
60
PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool,
 
61
                                             pj_bool_t high_quality,
 
62
                                             pj_bool_t large_filter,
 
63
                                             unsigned channel_count,
 
64
                                             unsigned rate_in,
 
65
                                             unsigned rate_out,
 
66
                                             unsigned samples_per_frame,
 
67
                                             pjmedia_resample **p_resample)
 
68
{
 
69
    pjmedia_resample *resample;
 
70
    int type, err;
 
71
 
 
72
    PJ_ASSERT_RETURN(pool && p_resample && rate_in &&
 
73
                     rate_out && samples_per_frame, PJ_EINVAL);
 
74
 
 
75
    resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample);
 
76
    PJ_ASSERT_RETURN(resample, PJ_ENOMEM);
 
77
 
 
78
    /* Select conversion type */
 
79
    if (high_quality) {
 
80
        type = large_filter ? SRC_SINC_BEST_QUALITY : SRC_SINC_MEDIUM_QUALITY;
 
81
    } else {
 
82
        type = large_filter ? SRC_SINC_FASTEST : SRC_LINEAR;
 
83
    }
 
84
 
 
85
    /* Create converter */
 
86
    resample->state = src_new(type, channel_count, &err);
 
87
    if (resample->state == NULL) {
 
88
        PJ_LOG(4,(THIS_FILE, "Error creating resample: %s", 
 
89
                  src_strerror(err)));
 
90
        return PJMEDIA_ERROR;
 
91
    }
 
92
 
 
93
    /* Calculate ratio */
 
94
    resample->ratio = rate_out * 1.0 / rate_in;
 
95
 
 
96
    /* Calculate number of samples for input and output */
 
97
    resample->in_samples = samples_per_frame;
 
98
    resample->out_samples = rate_out / (rate_in / samples_per_frame);
 
99
 
 
100
    resample->frame_in = (float*) 
 
101
                         pj_pool_calloc(pool, 
 
102
                                        resample->in_samples + 8, 
 
103
                                        sizeof(float));
 
104
 
 
105
    resample->frame_out = (float*) 
 
106
                          pj_pool_calloc(pool, 
 
107
                                         resample->out_samples + 8, 
 
108
                                         sizeof(float));
 
109
 
 
110
    /* Set the converter ratio */
 
111
    err = src_set_ratio(resample->state, resample->ratio);
 
112
    if (err != 0) {
 
113
        PJ_LOG(4,(THIS_FILE, "Error creating resample: %s", 
 
114
                  src_strerror(err)));
 
115
        return PJMEDIA_ERROR;
 
116
    }
 
117
 
 
118
    /* Done */
 
119
 
 
120
    PJ_LOG(5,(THIS_FILE, 
 
121
              "Resample using libsamplerate %s, type=%s (%s), "
 
122
              "ch=%d, in/out rate=%d/%d", 
 
123
              src_get_version(),
 
124
              src_get_name(type), src_get_description(type),
 
125
              channel_count, rate_in, rate_out));
 
126
 
 
127
    *p_resample = resample;
 
128
 
 
129
    return PJ_SUCCESS;
 
130
}
 
131
 
 
132
 
 
133
PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample,
 
134
                                   const pj_int16_t *input,
 
135
                                   pj_int16_t *output )
 
136
{
 
137
    SRC_DATA src_data;
 
138
 
 
139
    /* Convert samples to float */
 
140
    src_short_to_float_array(input, resample->frame_in, 
 
141
                             resample->in_samples);
 
142
 
 
143
    if (resample->in_extra) {
 
144
        unsigned i;
 
145
 
 
146
        for (i=0; i<resample->in_extra; ++i)
 
147
            resample->frame_in[resample->in_samples+i] =
 
148
                resample->frame_in[resample->in_samples-1];
 
149
    }
 
150
 
 
151
    /* Prepare SRC_DATA */
 
152
    pj_bzero(&src_data, sizeof(src_data));
 
153
    src_data.data_in = resample->frame_in;
 
154
    src_data.data_out = resample->frame_out;
 
155
    src_data.input_frames = resample->in_samples + resample->in_extra;
 
156
    src_data.output_frames = resample->out_samples + resample->out_extra;
 
157
    src_data.src_ratio = resample->ratio;
 
158
 
 
159
    /* Process! */
 
160
    src_process(resample->state, &src_data);
 
161
 
 
162
    /* Convert output back to short */
 
163
    src_float_to_short_array(resample->frame_out, output,
 
164
                             src_data.output_frames_gen);
 
165
 
 
166
    /* Replay last sample if conversion couldn't fill up the whole 
 
167
     * frame. This could happen for example with 22050 to 16000 conversion.
 
168
     */
 
169
    if (src_data.output_frames_gen < (int)resample->out_samples) {
 
170
        unsigned i;
 
171
 
 
172
        if (resample->in_extra < 4)
 
173
            resample->in_extra++;
 
174
 
 
175
        for (i=src_data.output_frames_gen; 
 
176
             i<resample->out_samples; ++i)
 
177
        {
 
178
            output[i] = output[src_data.output_frames_gen-1];
 
179
        }
 
180
    }
 
181
}
 
182
 
 
183
 
 
184
PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample)
 
185
{
 
186
    PJ_ASSERT_RETURN(resample != NULL, 0);
 
187
    return resample->in_samples;
 
188
}
 
189
 
 
190
 
 
191
PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample)
 
192
{
 
193
    PJ_ASSERT_ON_FAIL(resample, return);
 
194
    if (resample->state) {
 
195
        src_delete(resample->state);
 
196
        resample->state = NULL;
 
197
 
 
198
        PJ_LOG(5,(THIS_FILE, "Resample destroyed"));
 
199
    }
 
200
}
 
201
 
 
202
#else /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_LIBSAMPLERATE */
 
203
 
 
204
int pjmedia_libsamplerate_excluded;
 
205
 
 
206
#endif  /* PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_LIBSAMPLERATE */
 
207