2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/common.h"
27 #if defined(CONFIG_RESAMPLE_DBL)
28 #define SET_TYPE(func) func ## _dbl
32 #define OUT(d, v) d = v
33 #define DBL_TO_FELEM(d, v) d = v
34 #elif defined(CONFIG_RESAMPLE_FLT)
35 #define SET_TYPE(func) func ## _flt
39 #define OUT(d, v) d = v
40 #define DBL_TO_FELEM(d, v) d = v
41 #elif defined(CONFIG_RESAMPLE_S32)
42 #define SET_TYPE(func) func ## _s32
44 #define FELEM2 int64_t
45 #define FELEML int64_t
46 #define OUT(d, v) d = av_clipl_int32((v + (1 << 29)) >> 30)
47 #define DBL_TO_FELEM(d, v) d = av_clipl_int32(llrint(v * (1 << 30)));
49 #define SET_TYPE(func) func ## _s16
51 #define FELEM2 int32_t
52 #define FELEML int64_t
53 #define OUT(d, v) d = av_clip_int16((v + (1 << 14)) >> 15)
54 #define DBL_TO_FELEM(d, v) d = av_clip_int16(lrint(v * (1 << 15)))
57 static void SET_TYPE(resample_nearest
)(void *dst0
, int dst_index
, const void *src0
, unsigned int index
)
60 const FELEM
*src
= src0
;
61 dst
[dst_index
] = src
[index
];
64 static void SET_TYPE(resample_linear
)(ResampleContext
*c
, void *dst0
, int dst_index
,
65 const void *src0
, unsigned int index
, int frac
)
68 const FELEM
*src
= src0
;
70 unsigned int sample_index
= index
>> c
->phase_shift
;
72 FELEM
*filter
= ((FELEM
*)c
->filter_bank
) +
73 c
->filter_length
* (index
& c
->phase_mask
);
76 for (i
= 0; i
< c
->filter_length
; i
++) {
77 val
+= src
[sample_index
+ i
] * (FELEM2
)filter
[i
];
78 v2
+= src
[sample_index
+ i
] * (FELEM2
)filter
[i
+ c
->filter_length
];
80 val
+= (v2
- val
) * (FELEML
)frac
/ c
->src_incr
;
82 OUT(dst
[dst_index
], val
);
85 static void SET_TYPE(resample_one
)(ResampleContext
*c
,
86 void *dst0
, int dst_index
, const void *src0
,
87 unsigned int index
, int frac
)
90 const FELEM
*src
= src0
;
92 unsigned int sample_index
= index
>> c
->phase_shift
;
94 FELEM
*filter
= ((FELEM
*)c
->filter_bank
) +
95 c
->filter_length
* (index
& c
->phase_mask
);
97 for (i
= 0; i
< c
->filter_length
; i
++)
98 val
+= src
[sample_index
+ i
] * (FELEM2
)filter
[i
];
100 OUT(dst
[dst_index
], val
);
103 static void SET_TYPE(set_filter
)(void *filter0
, double *tab
, int phase
,
107 FELEM
*filter
= ((FELEM
*)filter0
) + phase
* tap_count
;
108 for (i
= 0; i
< tap_count
; i
++) {
109 DBL_TO_FELEM(filter
[i
], tab
[i
]);