3 * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #if defined(TEMPLATE_RESAMPLE_DBL)
30 # define RENAME(N) N ## _double
31 # define FILTER_SHIFT 0
34 # define FELEM2 double
36 # define OUT(d, v) d = v
38 #elif defined(TEMPLATE_RESAMPLE_FLT)
40 # define RENAME(N) N ## _float
41 # define FILTER_SHIFT 0
46 # define OUT(d, v) d = v
48 #elif defined(TEMPLATE_RESAMPLE_S32)
50 # define RENAME(N) N ## _int32
51 # define FILTER_SHIFT 30
52 # define DELEM int32_t
53 # define FELEM int32_t
54 # define FELEM2 int64_t
55 # define FELEM_MAX INT32_MAX
56 # define FELEM_MIN INT32_MIN
57 # define FOFFSET (1<<(FILTER_SHIFT-1))
58 # define OUT(d, v) (d) = av_clipl_int32((v)>>FILTER_SHIFT)
60 #elif defined(TEMPLATE_RESAMPLE_S16)
62 # define RENAME(N) N ## _int16
63 # define FILTER_SHIFT 15
64 # define DELEM int16_t
65 # define FELEM int16_t
66 # define FELEM2 int32_t
67 # define FELEML int64_t
68 # define FELEM_MAX INT16_MAX
69 # define FELEM_MIN INT16_MIN
70 # define FOFFSET (1<<(FILTER_SHIFT-1))
71 # define OUT(d, v) (d) = av_clip_int16((v)>>FILTER_SHIFT)
75 static void RENAME(resample_one)(void *dest, const void *source,
76 int dst_size, int64_t index2, int64_t incr)
79 const DELEM *src = source;
82 for (dst_index = 0; dst_index < dst_size; dst_index++) {
83 dst[dst_index] = src[index2 >> 32];
88 static int RENAME(resample_common)(ResampleContext *c,
89 void *dest, const void *source,
90 int n, int update_ctx)
93 const DELEM *src = source;
99 while (index >= c->phase_count) {
101 index -= c->phase_count;
104 for (dst_index = 0; dst_index < n; dst_index++) {
105 FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
107 FELEM2 val = FOFFSET;
110 for (i = 0; i + 1 < c->filter_length; i+=2) {
111 val += src[sample_index + i ] * (FELEM2)filter[i ];
112 val2 += src[sample_index + i + 1] * (FELEM2)filter[i + 1];
114 if (i < c->filter_length)
115 val += src[sample_index + i ] * (FELEM2)filter[i ];
117 OUT(dst[dst_index], val + (FELEML)val2);
119 OUT(dst[dst_index], val + val2);
122 frac += c->dst_incr_mod;
123 index += c->dst_incr_div;
124 if (frac >= c->src_incr) {
129 while (index >= c->phase_count) {
131 index -= c->phase_count;
143 static int RENAME(resample_linear)(ResampleContext *c,
144 void *dest, const void *source,
145 int n, int update_ctx)
148 const DELEM *src = source;
152 int sample_index = 0;
153 #if FILTER_SHIFT == 0
154 double inv_src_incr = 1.0 / c->src_incr;
157 while (index >= c->phase_count) {
159 index -= c->phase_count;
162 for (dst_index = 0; dst_index < n; dst_index++) {
163 FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
164 FELEM2 val = FOFFSET, v2 = FOFFSET;
167 for (i = 0; i < c->filter_length; i++) {
168 val += src[sample_index + i] * (FELEM2)filter[i];
169 v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
172 val += (v2 - val) * (FELEML) frac / c->src_incr;
174 # if FILTER_SHIFT == 0
175 val += (v2 - val) * inv_src_incr * frac;
177 val += (v2 - val) / c->src_incr * frac;
180 OUT(dst[dst_index], val);
182 frac += c->dst_incr_mod;
183 index += c->dst_incr_div;
184 if (frac >= c->src_incr) {
189 while (index >= c->phase_count) {
191 index -= c->phase_count;