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 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
32 int dst_incr_frac= c->dst_incr % c->src_incr;
33 int dst_incr= c->dst_incr / c->src_incr;
34 int compensation_distance= c->compensation_distance;
36 av_assert1(c->filter_shift == FILTER_SHIFT);
37 av_assert1(c->felem_size == sizeof(FELEM));
39 if(compensation_distance == 0 && c->filter_length == 1 && c->phase_shift==0){
40 int64_t index2= ((int64_t)index)<<32;
41 int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
42 dst_size= FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_incr / c->dst_incr);
44 for(dst_index=0; dst_index < dst_size; dst_index++){
45 dst[dst_index] = src[index2>>32];
48 index += dst_index * dst_incr;
49 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
50 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
51 av_assert2(index >= 0);
52 *consumed= index >> c->phase_shift;
53 index &= c->phase_mask;
54 }else if(compensation_distance == 0 && !c->linear && index >= 0){
56 for(dst_index=0; dst_index < dst_size; dst_index++){
58 sample_index += index >> c->phase_shift;
59 index &= c->phase_mask;
60 filter= ((FELEM*)c->filter_bank) + c->filter_alloc*index;
62 if(sample_index + c->filter_length > src_size){
69 for(i=0; i<c->filter_length; i++){
70 val += src[sample_index + i] * (FELEM2)filter[i];
72 OUT(dst[dst_index], val);
76 frac += dst_incr_frac;
78 if(frac >= c->src_incr){
83 *consumed = sample_index;
86 for(dst_index=0; dst_index < dst_size; dst_index++){
90 sample_index += index >> c->phase_shift;
91 index &= c->phase_mask;
92 filter = ((FELEM*)c->filter_bank) + c->filter_alloc*index;
94 if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
96 }else if(sample_index < 0){
97 for(i=0; i<c->filter_length; i++)
98 val += src[FFABS(sample_index + i)] * filter[i];
101 for(i=0; i<c->filter_length; i++){
102 val += src[sample_index + i] * (FELEM2)filter[i];
103 v2 += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
105 val+=(v2-val)*(FELEML)frac / c->src_incr;
107 for(i=0; i<c->filter_length; i++){
108 val += src[sample_index + i] * (FELEM2)filter[i];
112 OUT(dst[dst_index], val);
114 frac += dst_incr_frac;
116 if(frac >= c->src_incr){
121 if(dst_index + 1 == compensation_distance){
122 compensation_distance= 0;
123 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
124 dst_incr= c->ideal_dst_incr / c->src_incr;
127 *consumed= FFMAX(sample_index, 0);
128 index += FFMIN(sample_index, 0) << c->phase_shift;
131 if(compensation_distance){
132 compensation_distance -= dst_index;
133 av_assert1(compensation_distance > 0);
138 c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
139 c->compensation_distance= compensation_distance;
142 if(update_ctx && !c->compensation_distance){
144 av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
145 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);