FFmpeg
 All Data Structures Namespaces 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 
30 # define RENAME(N) N ## _double
31 # define FILTER_SHIFT 0
32 # define DELEM double
33 # define FELEM double
34 # define FELEM2 double
35 # define OUT(d, v) d = v
36 
37 #elif defined(TEMPLATE_RESAMPLE_FLT)
38 
39 # define RENAME(N) N ## _float
40 # define FILTER_SHIFT 0
41 # define DELEM float
42 # define FELEM float
43 # define FELEM2 float
44 # define OUT(d, v) d = v
45 
46 #elif defined(TEMPLATE_RESAMPLE_S32)
47 
48 # define RENAME(N) N ## _int32
49 # define FILTER_SHIFT 30
50 # define DELEM int32_t
51 # define FELEM int32_t
52 # define FELEM2 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) = av_clipl_int32(v)
57 
58 #elif defined(TEMPLATE_RESAMPLE_S16)
59 
60 # define RENAME(N) N ## _int16
61 # define FILTER_SHIFT 15
62 # define DELEM int16_t
63 # define FELEM int16_t
64 # define FELEM2 int32_t
65 # define FELEML int64_t
66 # define FELEM_MAX INT16_MAX
67 # define FELEM_MIN INT16_MIN
68 # define OUT(d, v) (v) = ((v) + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;\
69  (d) = av_clip_int16(v)
70 
71 #endif
72 
73 static void RENAME(resample_one)(void *dest, const void *source,
74  int dst_size, int64_t index2, int64_t incr)
75 {
76  DELEM *dst = dest;
77  const DELEM *src = source;
78  int dst_index;
79 
80  for (dst_index = 0; dst_index < dst_size; dst_index++) {
81  dst[dst_index] = src[index2 >> 32];
82  index2 += incr;
83  }
84 }
85 
87  void *dest, const void *source,
88  int n, int update_ctx)
89 {
90  DELEM *dst = dest;
91  const DELEM *src = source;
92  int dst_index;
93  int index= c->index;
94  int frac= c->frac;
95  int sample_index = 0;
96 
97  while (index >= c->phase_count) {
98  sample_index++;
99  index -= c->phase_count;
100  }
101 
102  for (dst_index = 0; dst_index < n; dst_index++) {
103  FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
104 
105  FELEM2 val=0;
106  int i;
107  for (i = 0; i < c->filter_length; i++) {
108  val += src[sample_index + i] * (FELEM2)filter[i];
109  }
110  OUT(dst[dst_index], val);
111 
112  frac += c->dst_incr_mod;
113  index += c->dst_incr_div;
114  if (frac >= c->src_incr) {
115  frac -= c->src_incr;
116  index++;
117  }
118 
119  while (index >= c->phase_count) {
120  sample_index++;
121  index -= c->phase_count;
122  }
123  }
124 
125  if(update_ctx){
126  c->frac= frac;
127  c->index= index;
128  }
129 
130  return sample_index;
131 }
132 
134  void *dest, const void *source,
135  int n, int update_ctx)
136 {
137  DELEM *dst = dest;
138  const DELEM *src = source;
139  int dst_index;
140  int index= c->index;
141  int frac= c->frac;
142  int sample_index = 0;
143 #if FILTER_SHIFT == 0
144  double inv_src_incr = 1.0 / c->src_incr;
145 #endif
146 
147  while (index >= c->phase_count) {
148  sample_index++;
149  index -= c->phase_count;
150  }
151 
152  for (dst_index = 0; dst_index < n; dst_index++) {
153  FELEM *filter = ((FELEM *) c->filter_bank) + c->filter_alloc * index;
154  FELEM2 val=0, v2 = 0;
155 
156  int i;
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 #ifdef FELEML
162  val += (v2 - val) * (FELEML) frac / c->src_incr;
163 #else
164 # if FILTER_SHIFT == 0
165  val += (v2 - val) * inv_src_incr * frac;
166 # else
167  val += (v2 - val) / c->src_incr * frac;
168 # endif
169 #endif
170  OUT(dst[dst_index], val);
171 
172  frac += c->dst_incr_mod;
173  index += c->dst_incr_div;
174  if (frac >= c->src_incr) {
175  frac -= c->src_incr;
176  index++;
177  }
178 
179  while (index >= c->phase_count) {
180  sample_index++;
181  index -= c->phase_count;
182  }
183  }
184 
185  if(update_ctx){
186  c->frac= frac;
187  c->index= index;
188  }
189 
190  return sample_index;
191 }
192 
193 #undef RENAME
194 #undef FILTER_SHIFT
195 #undef DELEM
196 #undef FELEM
197 #undef FELEM2
198 #undef FELEML
199 #undef FELEM_MAX
200 #undef FELEM_MIN
201 #undef OUT
const char const char void * val
Definition: avisynth_c.h:771
#define RENAME(name)
Definition: ffv1.h:205
#define src
Definition: vp8dsp.c:254
int16_t FELEM
Definition: resample2.c:37
int64_t FELEML
Definition: resample2.c:39
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
static void SET_TYPE() resample_linear(ResampleContext *c, void *dst0, int dst_index, const void *src0, unsigned int index, int frac)
static void SET_TYPE() resample_one(ResampleContext *c, void *dst0, int dst_index, const void *src0, unsigned int index, int frac)
int n
Definition: avisynth_c.h:684
int(* resample_common)(struct ResampleContext *c, void *dst, const void *src, int n, int update_ctx)
Definition: resample.h:56
int index
Definition: gxfenc.c:89
static AVCodec * c
int32_t FELEM2
Definition: resample2.c:38