]> git.sesse.net Git - ffmpeg/blob - libswresample/resample_template.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswresample / resample_template.c
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 int RENAME(swri_resample)(ResampleContext *c, DELEM *dst, const DELEM *src, int *consumed, int src_size, int dst_size, int update_ctx){
29     int dst_index, i;
30     int index= c->index;
31     int frac= c->frac;
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;
35
36     av_assert1(c->filter_shift == FILTER_SHIFT);
37     av_assert1(c->felem_size == sizeof(FELEM));
38
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);
43
44         for(dst_index=0; dst_index < dst_size; dst_index++){
45             dst[dst_index] = src[index2>>32];
46             index2 += incr;
47         }
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     }else if(compensation_distance == 0 && !c->linear && index >= 0){
52         for(dst_index=0; dst_index < dst_size; dst_index++){
53             FELEM *filter= ((FELEM*)c->filter_bank) + c->filter_alloc*(index & c->phase_mask);
54             int sample_index= index >> c->phase_shift;
55
56             if(sample_index + c->filter_length > src_size){
57                 break;
58             }else{
59                 FELEM2 val=0;
60                 for(i=0; i<c->filter_length; i++){
61                     val += src[sample_index + i] * (FELEM2)filter[i];
62                 }
63                 OUT(dst[dst_index], val);
64             }
65
66             frac += dst_incr_frac;
67             index += dst_incr;
68             if(frac >= c->src_incr){
69                 frac -= c->src_incr;
70                 index++;
71             }
72         }
73     }else{
74         for(dst_index=0; dst_index < dst_size; dst_index++){
75             FELEM *filter= ((FELEM*)c->filter_bank) + c->filter_alloc*(index & c->phase_mask);
76             int sample_index= index >> c->phase_shift;
77             FELEM2 val=0;
78
79             if(sample_index + c->filter_length > src_size || -sample_index >= src_size){
80                 break;
81             }else if(sample_index < 0){
82                 for(i=0; i<c->filter_length; i++)
83                     val += src[FFABS(sample_index + i)] * filter[i];
84             }else if(c->linear){
85                 FELEM2 v2=0;
86                 for(i=0; i<c->filter_length; i++){
87                     val += src[sample_index + i] * (FELEM2)filter[i];
88                     v2  += src[sample_index + i] * (FELEM2)filter[i + c->filter_alloc];
89                 }
90                 val+=(v2-val)*(FELEML)frac / c->src_incr;
91             }else{
92                 for(i=0; i<c->filter_length; i++){
93                     val += src[sample_index + i] * (FELEM2)filter[i];
94                 }
95             }
96
97             OUT(dst[dst_index], val);
98
99             frac += dst_incr_frac;
100             index += dst_incr;
101             if(frac >= c->src_incr){
102                 frac -= c->src_incr;
103                 index++;
104             }
105
106             if(dst_index + 1 == compensation_distance){
107                 compensation_distance= 0;
108                 dst_incr_frac= c->ideal_dst_incr % c->src_incr;
109                 dst_incr=      c->ideal_dst_incr / c->src_incr;
110             }
111         }
112     }
113     *consumed= FFMAX(index, 0) >> c->phase_shift;
114     if(index>=0) index &= c->phase_mask;
115
116     if(compensation_distance){
117         compensation_distance -= dst_index;
118         av_assert1(compensation_distance > 0);
119     }
120     if(update_ctx){
121         c->frac= frac;
122         c->index= index;
123         c->dst_incr= dst_incr_frac + c->src_incr*dst_incr;
124         c->compensation_distance= compensation_distance;
125     }
126 #if 0
127     if(update_ctx && !c->compensation_distance){
128 #undef rand
129         av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
130 av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c->compensation_distance);
131     }
132 #endif
133
134     return dst_index;
135 }