FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
resample_template.c
Go to the documentation of this file.
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 # define RENAME(N) N ## _double
30 # define FILTER_SHIFT 0
31 # define DELEM double
32 # define FELEM double
33 # define FELEM2 double
34 # define FELEML double
35 # define OUT(d, v) d = v
36 
37 #elif defined(TEMPLATE_RESAMPLE_FLT)
38 # define RENAME(N) N ## _float
39 # define FILTER_SHIFT 0
40 # define DELEM float
41 # define FELEM float
42 # define FELEM2 float
43 # define FELEML float
44 # define OUT(d, v) d = v
45 
46 #elif defined(TEMPLATE_RESAMPLE_S32)
47 # define RENAME(N) N ## _int32
48 # define FILTER_SHIFT 30
49 # define DELEM int32_t
50 # define FELEM int32_t
51 # define FELEM2 int64_t
52 # define FELEML int64_t
53 # define FELEM_MAX INT32_MAX
54 # define FELEM_MIN INT32_MIN
55 # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
56  d = (uint64_t)(v + 0x80000000) > 0xFFFFFFFF ? (v>>63) ^ 0x7FFFFFFF : v
57 
58 #elif defined(TEMPLATE_RESAMPLE_S16) \
59  || defined(TEMPLATE_RESAMPLE_S16_MMX2) \
60  || defined(TEMPLATE_RESAMPLE_S16_SSSE3)
61 
62 # define FILTER_SHIFT 15
63 # define DELEM int16_t
64 # define FELEM int16_t
65 # define FELEM2 int32_t
66 # define FELEML int64_t
67 # define FELEM_MAX INT16_MAX
68 # define FELEM_MIN INT16_MIN
69 # define OUT(d, v) v = (v + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
70  d = (unsigned)(v + 32768) > 65535 ? (v>>31) ^ 32767 : v
71 
72 # if defined(TEMPLATE_RESAMPLE_S16)
73 # define RENAME(N) N ## _int16
74 # elif defined(TEMPLATE_RESAMPLE_S16_MMX2)
75 # define COMMON_CORE COMMON_CORE_INT16_MMX2
76 # define RENAME(N) N ## _int16_mmx2
77 # elif defined(TEMPLATE_RESAMPLE_S16_SSSE3)
78 # define COMMON_CORE COMMON_CORE_INT16_SSSE3
79 # define RENAME(N) N ## _int16_ssse3
80 # endif
81 
82 #endif
83 
84 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
85  int dst_index, i;
86  int index= c->index;
87  int frac= c->frac;
88  int dst_incr_frac= c->dst_incr % c->src_incr;
89  int dst_incr= c->dst_incr / c->src_incr;
90  int compensation_distance= c->compensation_distance;
91 
92  av_assert1(c->filter_shift == FILTER_SHIFT);
93  av_assert1(c->felem_size == sizeof(FELEM));
94 
95  if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
96  int64_t index2= ((int64_t)index)<<32;
97  int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
98  dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
99 
100  for(dst_index=0; dst_index < dst_size; dst_index++){
101  dst[dst_index] = src[index2>>32];
102  index2 += incr;
103  }
104  index += dst_index * dst_incr;
105  index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
106  frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
107  av_assert2(index >= 0);
108  *consumed= index >> c->phase_shift;
109  index &= c->phase_mask;
110  }else if(compensation_distance == 0 && !c->linear && index >= 0){
111  int sample_index = 0;
112  for(dst_index=0; dst_index < dst_size; dst_index++){
113  FELEM *filter;
114  sample_index += index >> c->phase_shift;
115  index &= c->phase_mask;
116  filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
117 
118  if(sample_index + c->filter_length > src_size){
119  break;
120  }else{
121 #ifdef COMMON_CORE
122  COMMON_CORE
123 #else
124  FELEM2 val=0;
125  for(i=0; i<c->filter_length; i++){
126  val += src[sample_index + i] * (FELEM2)filter[i];
127  }
128  OUT(dst[dst_index], val);
129 #endif
130  }
131 
132  frac += dst_incr_frac;
133  index += dst_incr;
134  if(frac >= c->src_incr){
135  frac -= c->src_incr;
136  index++;
137  }
138  }
139  *consumed = sample_index;
140  }else{
141  int sample_index = 0;
142  for(dst_index=0; dst_index < dst_size; dst_index++){
143  FELEM *filter;
144  FELEM2 val=0;
145 
146  sample_index += index >> c->phase_shift;
147  index &= c->phase_mask;
148  filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
149 
150  if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
151  break;
152  }else if(sample_index < 0){
153  for(i=0; i<c->filter_length; i++)
154  val += src[FFABS(sample_index + i)] * filter[i];
155  }else if(c->linear){
156  FELEM2 v2=0;
157  for(i=0; i<c->filter_length; i++){
158  val += src[sample_index + i] * (FELEM2)filter[i];
159  v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
160  }
161  val+=(v2-val)*(FELEML)frac / c->src_incr;
162  }else{
163  for(i=0; i<c->filter_length; i++){
164  val += src[sample_index + i] * (FELEM2)filter[i];
165  }
166  }
167 
168  OUT(dst[dst_index], val);
169 
170  frac += dst_incr_frac;
171  index += dst_incr;
172  if(frac >= c->src_incr){
173  frac -= c->src_incr;
174  index++;
175  }
176 
177  if(dst_index + 1 == compensation_distance){
178  compensation_distance= 0;
179  dst_incr_frac= c->ideal_dst_incr % c->src_incr;
180  dst_incr= c->ideal_dst_incr / c->src_incr;
181  }
182  }
183  *consumed= FFMAX(sample_index, 0);
184  index += FFMIN(sample_index, 0) << c->phase_shift;
185 
186  if(compensation_distance){
187  compensation_distance -= dst_index;
188  av_assert1(compensation_distance > 0);
189  }
190  }
191 
192  if(update_ctx){
193  c->frac= frac;
194  c->index= index;
195  c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
196  c->compensation_distance= compensation_distance;
197  }
198 
199  return dst_index;
200 }
201 
202 #undef COMMON_CORE
203 #undef RENAME
204 #undef FILTER_SHIFT
205 #undef DELEM
206 #undef FELEM
207 #undef FELEM2
208 #undef FELEML
209 #undef FELEM_MAX
210 #undef FELEM_MIN
211 #undef OUT