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)
29 # define RENAME(N) N ## _double
30 # define FILTER_SHIFT 0
33 # define FELEM2 double
34 # define FELEML double
35 # define OUT(d, v) d = v
37 #elif defined(TEMPLATE_RESAMPLE_FLT) \
38 || defined(TEMPLATE_RESAMPLE_FLT_SSE)
40 # define FILTER_SHIFT 0
45 # define OUT(d, v) d = v
47 # if defined(TEMPLATE_RESAMPLE_FLT)
48 # define RENAME(N) N ## _float
49 # elif defined(TEMPLATE_RESAMPLE_FLT_SSE)
50 # define COMMON_CORE COMMON_CORE_FLT_SSE
51 # define RENAME(N) N ## _float_sse
54 #elif defined(TEMPLATE_RESAMPLE_S32)
55 # define RENAME(N) N ## _int32
56 # define FILTER_SHIFT 30
57 # define DELEM int32_t
58 # define FELEM int32_t
59 # define FELEM2 int64_t
60 # define FELEML int64_t
61 # define FELEM_MAX INT32_MAX
62 # define FELEM_MIN INT32_MIN
63 # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
64 d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
66 #elif defined(TEMPLATE_RESAMPLE_S16) \
67 || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
68 || defined(TEMPLATE_RESAMPLE_S16_SSE2)
70 # define FILTER_SHIFT 15
71 # define DELEM int16_t
72 # define FELEM int16_t
73 # define FELEM2 int32_t
74 # define FELEML int64_t
75 # define FELEM_MAX INT16_MAX
76 # define FELEM_MIN INT16_MIN
77 # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
78 d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
80 # if defined(TEMPLATE_RESAMPLE_S16)
81 # define RENAME(N) N ## _int16
82 # elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
83 # define COMMON_CORE COMMON_CORE_INT16_MMX2
84 # define RENAME(N) N ## _int16_mmx2
85 # elif defined(TEMPLATE_RESAMPLE_S16_SSE2)
86 # define COMMON_CORE COMMON_CORE_INT16_SSE2
87 # define RENAME(N) N ## _int16_sse2
92 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
96 int dst_incr_frac= c->dst_incr % c->src_incr;
97 int dst_incr= c->dst_incr / c->src_incr;
98 int compensation_distance= c->compensation_distance;
100 av_assert1(c->filter_shift == FILTER_SHIFT);
101 av_assert1(c->felem_size == sizeof(FELEM));
103 if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
104 int64_t index2= ((int64_t)index)<<32;
105 int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
106 dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
108 for(dst_index=0; dst_index < dst_size; dst_index++){
109 dst[dst_index] = src[index2>>32];
112 index += dst_index * dst_incr;
113 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
114 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
115 av_assert2(index >= 0);
116 *consumed= index >> c->phase_shift;
117 index &= c->phase_mask;
118 }else if(compensation_distance == 0 && !c->linear && index >= 0){
119 int sample_index = 0;
120 for(dst_index=0; dst_index < dst_size; dst_index++){
122 sample_index += index >> c->phase_shift;
123 index &= c->phase_mask;
124 filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
126 if(sample_index + c->filter_length > src_size){
133 for(i=0; i<c->filter_length; i++){
134 val += src[sample_index + i] * (FELEM2)filter[i];
136 OUT(dst[dst_index], val);
140 frac += dst_incr_frac;
142 if(frac >= c->src_incr){
147 *consumed = sample_index;
149 int sample_index = 0;
150 for(dst_index=0; dst_index < dst_size; dst_index++){
154 sample_index += index >> c->phase_shift;
155 index &= c->phase_mask;
156 filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
158 if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
160 }else if(sample_index < 0){
161 for(i=0; i<c->filter_length; i++)
162 val += src[FFABS(sample_index + i)] * (FELEM2)filter[i];
163 OUT(dst[dst_index], val);
166 for(i=0; i<c->filter_length; i++){
167 val += src[sample_index + i] * (FELEM2)filter[i];
168 v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
170 val+=(v2-val)*(FELEML)frac / c->src_incr;
171 OUT(dst[dst_index], val);
176 for(i=0; i<c->filter_length; i++){
177 val += src[sample_index + i] * (FELEM2)filter[i];
179 OUT(dst[dst_index], val);
183 frac += dst_incr_frac;
185 if(frac >= c->src_incr){
190 if(dst_index + 1 == compensation_distance){
191 compensation_distance= 0;
192 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
193 dst_incr= c->ideal_dst_incr / c->src_incr;
196 *consumed= FFMAX(sample_index, 0);
197 index += FFMIN(sample_index, 0) << c->phase_shift;
199 if(compensation_distance){
200 compensation_distance -= dst_index;
201 av_assert1(compensation_distance > 0);
208 c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
209 c->compensation_distance= compensation_distance;