]> git.sesse.net Git - ffmpeg/blob - libswresample/resample_template.c
Merge commit '5ec6d152e26c570c0a16ec72c1f354db95708179'
[ffmpeg] / libswresample / resample_template.c
1 /*
2  * audio resampling
3  * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * audio resampling
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #if    defined(TEMPLATE_RESAMPLE_DBL)     \
29     || defined(TEMPLATE_RESAMPLE_DBL_SSE2)
30
31 #    define FILTER_SHIFT 0
32 #    define DELEM  double
33 #    define FELEM  double
34 #    define FELEM2 double
35 #    define FELEML double
36 #    define OUT(d, v) d = v
37
38 #    if defined(TEMPLATE_RESAMPLE_DBL)
39 #        define RENAME(N) N ## _double
40 #    elif defined(TEMPLATE_RESAMPLE_DBL_SSE2)
41 #        define COMMON_CORE COMMON_CORE_DBL_SSE2
42 #        define LINEAR_CORE LINEAR_CORE_DBL_SSE2
43 #        define RENAME(N) N ## _double_sse2
44 #    endif
45
46 #elif    defined(TEMPLATE_RESAMPLE_FLT)     \
47       || defined(TEMPLATE_RESAMPLE_FLT_SSE) \
48       || defined(TEMPLATE_RESAMPLE_FLT_AVX)
49
50 #    define FILTER_SHIFT 0
51 #    define DELEM  float
52 #    define FELEM  float
53 #    define FELEM2 float
54 #    define FELEML float
55 #    define OUT(d, v) d = v
56
57 #    if defined(TEMPLATE_RESAMPLE_FLT)
58 #        define RENAME(N) N ## _float
59 #    elif defined(TEMPLATE_RESAMPLE_FLT_SSE)
60 #        define COMMON_CORE COMMON_CORE_FLT_SSE
61 #        define LINEAR_CORE LINEAR_CORE_FLT_SSE
62 #        define RENAME(N) N ## _float_sse
63 #    elif defined(TEMPLATE_RESAMPLE_FLT_AVX)
64 #        define COMMON_CORE COMMON_CORE_FLT_AVX
65 #        define LINEAR_CORE LINEAR_CORE_FLT_AVX
66 #        define RENAME(N) N ## _float_avx
67 #    endif
68
69 #elif defined(TEMPLATE_RESAMPLE_S32)
70 #    define RENAME(N) N ## _int32
71 #    define FILTER_SHIFT 30
72 #    define DELEM  int32_t
73 #    define FELEM  int32_t
74 #    define FELEM2 int64_t
75 #    define FELEML int64_t
76 #    define FELEM_MAX INT32_MAX
77 #    define FELEM_MIN INT32_MIN
78 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
79                       d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
80
81 #elif    defined(TEMPLATE_RESAMPLE_S16)      \
82       || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
83       || defined(TEMPLATE_RESAMPLE_S16_SSE2)
84
85 #    define FILTER_SHIFT 15
86 #    define DELEM  int16_t
87 #    define FELEM  int16_t
88 #    define FELEM2 int32_t
89 #    define FELEML int64_t
90 #    define FELEM_MAX INT16_MAX
91 #    define FELEM_MIN INT16_MIN
92 #    define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
93                       d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
94
95 #    if defined(TEMPLATE_RESAMPLE_S16)
96 #        define RENAME(N) N ## _int16
97 #    elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
98 #        define COMMON_CORE COMMON_CORE_INT16_MMX2
99 #        define LINEAR_CORE LINEAR_CORE_INT16_MMX2
100 #        define RENAME(N) N ## _int16_mmx2
101 #    elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
102 #        define COMMON_CORE COMMON_CORE_INT16_SSE2
103 #        define LINEAR_CORE LINEAR_CORE_INT16_SSE2
104 #        define RENAME(N) N ## _int16_sse2
105 #    endif
106
107 #endif
108
109 #if DO_RESAMPLE_ONE
110 static void RENAME(resample_one)(DELEM *dst, const DELEM *src,
111                                  int dst_size, int64_t index2, int64_t incr)
112 {
113     int dst_index;
114
115     for (dst_index = 0; dst_index < dst_size; dst_index++) {
116         dst[dst_index] = src[index2 >> 32];
117         index2 += incr;
118     }
119 }
120 #endif
121
122 int RENAME(swri_resample_common)(ResampleContext *c,
123                                  DELEM *dst, const DELEM *src,
124                                  int n, int update_ctx)
125 {
126     int dst_index;
127     int index= c->index;
128     int frac= c->frac;
129     int sample_index = index >> c->phase_shift;
130
131     index &= c->phase_mask;
132     for (dst_index = 0; dst_index < n; dst_index++) {
133         FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
134
135 #ifdef COMMON_CORE
136         COMMON_CORE
137 #else
138         FELEM2 val=0;
139         int i;
140         for (i = 0; i < c->filter_length; i++) {
141             val += src[sample_index + i] * (FELEM2)filter[i];
142         }
143         OUT(dst[dst_index], val);
144 #endif
145
146         frac  += c->dst_incr_mod;
147         index += c->dst_incr_div;
148         if (frac >= c->src_incr) {
149             frac -= c->src_incr;
150             index++;
151         }
152         sample_index += index >> c->phase_shift;
153         index &= c->phase_mask;
154     }
155
156     if(update_ctx){
157         c->frac= frac;
158         c->index= index;
159     }
160
161     return sample_index;
162 }
163
164 int RENAME(swri_resample_linear)(ResampleContext *c,
165                                  DELEM *dst, const DELEM *src,
166                                  int n, int update_ctx)
167 {
168     int dst_index;
169     int index= c->index;
170     int frac= c->frac;
171     int sample_index = index >> c->phase_shift;
172
173     index &= c->phase_mask;
174     for (dst_index = 0; dst_index < n; dst_index++) {
175         FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
176         FELEM2 val=0, v2 = 0;
177
178 #ifdef LINEAR_CORE
179         LINEAR_CORE
180 #else
181         int i;
182         for (i = 0; i < c->filter_length; i++) {
183             val += src[sample_index + i] * (FELEM2)filter[i];
184             v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
185         }
186 #endif
187         val += (v2 - val) * (FELEML) frac / c->src_incr;
188         OUT(dst[dst_index], val);
189
190         frac += c->dst_incr_mod;
191         index += c->dst_incr_div;
192         if (frac >= c->src_incr) {
193             frac -= c->src_incr;
194             index++;
195         }
196         sample_index += index >> c->phase_shift;
197         index &= c->phase_mask;
198     }
199
200     if(update_ctx){
201         c->frac= frac;
202         c->index= index;
203     }
204
205     return sample_index;
206 }
207
208 #undef COMMON_CORE
209 #undef LINEAR_CORE
210 #undef RENAME
211 #undef FILTER_SHIFT
212 #undef DELEM
213 #undef FELEM
214 #undef FELEM2
215 #undef FELEML
216 #undef FELEM_MAX
217 #undef FELEM_MIN
218 #undef OUT