Mercurial > mplayer.hg
annotate libaf/af_lavcresample.c @ 25944:8a1c062707ff
Do not uselessly erase background, OpenGL will take care of drawing everything.
author | reimar |
---|---|
date | Sun, 10 Feb 2008 19:47:54 +0000 |
parents | 867ee1c2114b |
children | 1318e956c092 |
rev | line source |
---|---|
25529
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
1 /* |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
3 * |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
4 * This file is part of MPlayer. |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
5 * |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
6 * MPlayer is free software; you can redistribute it and/or modify |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
7 * it under the terms of the GNU General Public License as published by |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
8 * the Free Software Foundation; either version 2 of the License, or |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
9 * (at your option) any later version. |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
10 * |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
11 * MPlayer is distributed in the hope that it will be useful, |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
14 * GNU General Public License for more details. |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
15 * |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
16 * You should have received a copy of the GNU General Public License |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
17 * along with MPlayer; if not, write to the Free Software |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
867ee1c2114b
Relicense GPL v2 files as GPL v2+ and add proper license headers.
diego
parents:
24900
diff
changeset
|
19 */ |
13713 | 20 |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <string.h> | |
24 #include <inttypes.h> | |
25 | |
16982 | 26 #include "config.h" |
13713 | 27 #include "af.h" |
28 | |
13859
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
29 #ifdef USE_LIBAVCODEC_SO |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
30 #include <ffmpeg/avcodec.h> |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
31 #include <ffmpeg/rational.h> |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
32 #else |
16168
99c188fbdba4
libavutil compile fix (working also with old libavcodec)
reimar
parents:
16167
diff
changeset
|
33 #include "avcodec.h" |
99c188fbdba4
libavutil compile fix (working also with old libavcodec)
reimar
parents:
16167
diff
changeset
|
34 #include "rational.h" |
13859
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
35 #endif |
13713 | 36 |
37 // Data for specific instances of this filter | |
38 typedef struct af_resample_s{ | |
39 struct AVResampleContext *avrctx; | |
22178
c4d9550c9faf
Use AF_NCH for max number of channels instead of private CHANS define.
uau
parents:
17522
diff
changeset
|
40 int16_t *in[AF_NCH]; |
13713 | 41 int in_alloc; |
42 int index; | |
43 | |
44 int filter_length; | |
45 int linear; | |
46 int phase_shift; | |
13730 | 47 double cutoff; |
13713 | 48 }af_resample_t; |
49 | |
50 | |
51 // Initialization and runtime control | |
52 static int control(struct af_instance_s* af, int cmd, void* arg) | |
53 { | |
54 af_resample_t* s = (af_resample_t*)af->setup; | |
55 af_data_t *data= (af_data_t*)arg; | |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
56 int out_rate, test_output_res; // helpers for checking input format |
13713 | 57 |
58 switch(cmd){ | |
59 case AF_CONTROL_REINIT: | |
60 if((af->data->rate == data->rate) || (af->data->rate == 0)) | |
61 return AF_DETACH; | |
62 | |
63 af->data->nch = data->nch; | |
22178
c4d9550c9faf
Use AF_NCH for max number of channels instead of private CHANS define.
uau
parents:
17522
diff
changeset
|
64 if (af->data->nch > AF_NCH) af->data->nch = AF_NCH; |
14245 | 65 af->data->format = AF_FORMAT_S16_NE; |
13713 | 66 af->data->bps = 2; |
24888 | 67 af->mul = (double)af->data->rate / data->rate; |
24900 | 68 af->delay = af->data->nch * s->filter_length / min(af->mul, 1); // *bps*.5 |
13713 | 69 |
70 if(s->avrctx) av_resample_close(s->avrctx); | |
24888 | 71 s->avrctx= av_resample_init(af->data->rate, /*in_rate*/data->rate, s->filter_length, s->phase_shift, s->linear, s->cutoff); |
13713 | 72 |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
73 // hack to make af_test_output ignore the samplerate change |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
74 out_rate = af->data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
75 af->data->rate = data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
76 test_output_res = af_test_output(af, (af_data_t*)arg); |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
77 af->data->rate = out_rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
78 return test_output_res; |
13713 | 79 case AF_CONTROL_COMMAND_LINE:{ |
22755 | 80 s->cutoff= 0.0; |
13730 | 81 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); |
22755 | 82 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); |
13713 | 83 return AF_OK; |
84 } | |
85 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
86 af->data->rate = *(int*)arg; | |
87 return AF_OK; | |
88 } | |
89 return AF_UNKNOWN; | |
90 } | |
91 | |
92 // Deallocate memory | |
93 static void uninit(struct af_instance_s* af) | |
94 { | |
95 if(af->data) | |
22179 | 96 free(af->data->audio); |
97 free(af->data); | |
13713 | 98 if(af->setup){ |
22182 | 99 int i; |
13713 | 100 af_resample_t *s = af->setup; |
101 if(s->avrctx) av_resample_close(s->avrctx); | |
22181
e99ebf35812b
Cosmetics: remove tabs added in last commit from otherwise tab-free file.
reimar
parents:
22179
diff
changeset
|
102 for (i=0; i < AF_NCH; i++) |
e99ebf35812b
Cosmetics: remove tabs added in last commit from otherwise tab-free file.
reimar
parents:
22179
diff
changeset
|
103 free(s->in[i]); |
13713 | 104 free(s); |
105 } | |
106 } | |
107 | |
108 // Filter data through filter | |
109 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
110 { | |
111 af_resample_t *s = af->setup; | |
112 int i, j, consumed, ret; | |
113 int16_t *in = (int16_t*)data->audio; | |
114 int16_t *out; | |
115 int chans = data->nch; | |
116 int in_len = data->len/(2*chans); | |
24888 | 117 int out_len = in_len * af->mul + 10; |
22178
c4d9550c9faf
Use AF_NCH for max number of channels instead of private CHANS define.
uau
parents:
17522
diff
changeset
|
118 int16_t tmp[AF_NCH][out_len]; |
13713 | 119 |
120 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
121 return NULL; | |
122 | |
123 out= (int16_t*)af->data->audio; | |
13730 | 124 |
125 out_len= min(out_len, af->data->len/(2*chans)); | |
126 | |
13713 | 127 if(s->in_alloc < in_len + s->index){ |
128 s->in_alloc= in_len + s->index; | |
129 for(i=0; i<chans; i++){ | |
22179 | 130 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); |
13713 | 131 } |
132 } | |
133 | |
14283 | 134 if(chans==1){ |
135 memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t)); | |
136 }else if(chans==2){ | |
137 for(j=0; j<in_len; j++){ | |
138 s->in[0][j + s->index]= *(in++); | |
139 s->in[1][j + s->index]= *(in++); | |
140 } | |
141 }else{ | |
142 for(j=0; j<in_len; j++){ | |
143 for(i=0; i<chans; i++){ | |
144 s->in[i][j + s->index]= *(in++); | |
145 } | |
13713 | 146 } |
147 } | |
148 in_len += s->index; | |
149 | |
150 for(i=0; i<chans; i++){ | |
151 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); | |
152 } | |
153 out_len= ret; | |
154 | |
155 s->index= in_len - consumed; | |
156 for(i=0; i<chans; i++){ | |
157 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t)); | |
158 } | |
159 | |
14283 | 160 if(chans==1){ |
161 memcpy(out, tmp[0], out_len*sizeof(int16_t)); | |
162 }else if(chans==2){ | |
163 for(j=0; j<out_len; j++){ | |
164 *(out++)= tmp[0][j]; | |
165 *(out++)= tmp[1][j]; | |
166 } | |
167 }else{ | |
168 for(j=0; j<out_len; j++){ | |
169 for(i=0; i<chans; i++){ | |
170 *(out++)= tmp[i][j]; | |
171 } | |
13713 | 172 } |
173 } | |
174 | |
175 data->audio = af->data->audio; | |
176 data->len = out_len*chans*2; | |
177 data->rate = af->data->rate; | |
178 return data; | |
179 } | |
180 | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22182
diff
changeset
|
181 static int af_open(af_instance_t* af){ |
14186
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
182 af_resample_t *s = calloc(1,sizeof(af_resample_t)); |
13713 | 183 af->control=control; |
184 af->uninit=uninit; | |
185 af->play=play; | |
24888 | 186 af->mul=1; |
13713 | 187 af->data=calloc(1,sizeof(af_data_t)); |
14186
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
188 s->filter_length= 16; |
22755 | 189 s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); |
14186
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
190 s->phase_shift= 10; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
191 // s->setup = RSMP_INT | FREQ_SLOPPY; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
192 af->setup=s; |
13713 | 193 return AF_OK; |
194 } | |
195 | |
196 af_info_t af_info_lavcresample = { | |
197 "Sample frequency conversion using libavcodec", | |
198 "lavcresample", | |
199 "Michael Niedermayer", | |
200 "", | |
201 AF_FLAGS_REENTRANT, | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22182
diff
changeset
|
202 af_open |
13713 | 203 }; |