Mercurial > mplayer.hg
annotate libaf/af_lavcresample.c @ 23672:9b03f561f65e
synced with r23579
author | ptt |
---|---|
date | Fri, 29 Jun 2007 14:03:41 +0000 |
parents | cc3baf55288d |
children | b2402b4f0afa |
rev | line source |
---|---|
13713 | 1 // Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
2 // #inlcude <GPL_v2.h> | |
3 | |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <string.h> | |
7 #include <inttypes.h> | |
8 | |
16982 | 9 #include "config.h" |
13713 | 10 #include "af.h" |
11 | |
13859
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
12 #ifdef USE_LIBAVCODEC_SO |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
13 #include <ffmpeg/avcodec.h> |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
14 #include <ffmpeg/rational.h> |
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
15 #else |
16168
99c188fbdba4
libavutil compile fix (working also with old libavcodec)
reimar
parents:
16167
diff
changeset
|
16 #include "avcodec.h" |
99c188fbdba4
libavutil compile fix (working also with old libavcodec)
reimar
parents:
16167
diff
changeset
|
17 #include "rational.h" |
13859
1b0d5a6ab7dc
libavcodec.so headers patch by (Glenn Washburn <glenniii at mail dot utexas dot edu>)
michael
parents:
13730
diff
changeset
|
18 #endif |
13713 | 19 |
20 // Data for specific instances of this filter | |
21 typedef struct af_resample_s{ | |
22 struct AVResampleContext *avrctx; | |
22178
c4d9550c9faf
Use AF_NCH for max number of channels instead of private CHANS define.
uau
parents:
17522
diff
changeset
|
23 int16_t *in[AF_NCH]; |
13713 | 24 int in_alloc; |
25 int index; | |
26 | |
27 int filter_length; | |
28 int linear; | |
29 int phase_shift; | |
13730 | 30 double cutoff; |
13713 | 31 }af_resample_t; |
32 | |
33 | |
34 // Initialization and runtime control | |
35 static int control(struct af_instance_s* af, int cmd, void* arg) | |
36 { | |
37 af_resample_t* s = (af_resample_t*)af->setup; | |
38 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
|
39 int out_rate, test_output_res; // helpers for checking input format |
13713 | 40 |
41 switch(cmd){ | |
42 case AF_CONTROL_REINIT: | |
43 if((af->data->rate == data->rate) || (af->data->rate == 0)) | |
44 return AF_DETACH; | |
45 | |
46 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
|
47 if (af->data->nch > AF_NCH) af->data->nch = AF_NCH; |
14245 | 48 af->data->format = AF_FORMAT_S16_NE; |
13713 | 49 af->data->bps = 2; |
14433
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
50 af->mul.n = af->data->rate; |
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
51 af->mul.d = data->rate; |
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
52 af_frac_cancel(&af->mul); |
13730 | 53 af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate); |
13713 | 54 |
55 if(s->avrctx) av_resample_close(s->avrctx); | |
13730 | 56 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear, s->cutoff); |
13713 | 57 |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
58 // 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
|
59 out_rate = af->data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
60 af->data->rate = data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
61 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
|
62 af->data->rate = out_rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
63 return test_output_res; |
13713 | 64 case AF_CONTROL_COMMAND_LINE:{ |
22755 | 65 s->cutoff= 0.0; |
13730 | 66 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); |
22755 | 67 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); |
13713 | 68 return AF_OK; |
69 } | |
70 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
71 af->data->rate = *(int*)arg; | |
72 return AF_OK; | |
73 } | |
74 return AF_UNKNOWN; | |
75 } | |
76 | |
77 // Deallocate memory | |
78 static void uninit(struct af_instance_s* af) | |
79 { | |
80 if(af->data) | |
22179 | 81 free(af->data->audio); |
82 free(af->data); | |
13713 | 83 if(af->setup){ |
22182 | 84 int i; |
13713 | 85 af_resample_t *s = af->setup; |
86 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
|
87 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
|
88 free(s->in[i]); |
13713 | 89 free(s); |
90 } | |
91 } | |
92 | |
93 // Filter data through filter | |
94 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
95 { | |
96 af_resample_t *s = af->setup; | |
97 int i, j, consumed, ret; | |
98 int16_t *in = (int16_t*)data->audio; | |
99 int16_t *out; | |
100 int chans = data->nch; | |
101 int in_len = data->len/(2*chans); | |
102 int out_len = (in_len*af->mul.n) / af->mul.d + 10; | |
22178
c4d9550c9faf
Use AF_NCH for max number of channels instead of private CHANS define.
uau
parents:
17522
diff
changeset
|
103 int16_t tmp[AF_NCH][out_len]; |
13713 | 104 |
105 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
106 return NULL; | |
107 | |
108 out= (int16_t*)af->data->audio; | |
13730 | 109 |
110 out_len= min(out_len, af->data->len/(2*chans)); | |
111 | |
13713 | 112 if(s->in_alloc < in_len + s->index){ |
113 s->in_alloc= in_len + s->index; | |
114 for(i=0; i<chans; i++){ | |
22179 | 115 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); |
13713 | 116 } |
117 } | |
118 | |
14283 | 119 if(chans==1){ |
120 memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t)); | |
121 }else if(chans==2){ | |
122 for(j=0; j<in_len; j++){ | |
123 s->in[0][j + s->index]= *(in++); | |
124 s->in[1][j + s->index]= *(in++); | |
125 } | |
126 }else{ | |
127 for(j=0; j<in_len; j++){ | |
128 for(i=0; i<chans; i++){ | |
129 s->in[i][j + s->index]= *(in++); | |
130 } | |
13713 | 131 } |
132 } | |
133 in_len += s->index; | |
134 | |
135 for(i=0; i<chans; i++){ | |
136 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); | |
137 } | |
138 out_len= ret; | |
139 | |
140 s->index= in_len - consumed; | |
141 for(i=0; i<chans; i++){ | |
142 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t)); | |
143 } | |
144 | |
14283 | 145 if(chans==1){ |
146 memcpy(out, tmp[0], out_len*sizeof(int16_t)); | |
147 }else if(chans==2){ | |
148 for(j=0; j<out_len; j++){ | |
149 *(out++)= tmp[0][j]; | |
150 *(out++)= tmp[1][j]; | |
151 } | |
152 }else{ | |
153 for(j=0; j<out_len; j++){ | |
154 for(i=0; i<chans; i++){ | |
155 *(out++)= tmp[i][j]; | |
156 } | |
13713 | 157 } |
158 } | |
159 | |
160 data->audio = af->data->audio; | |
161 data->len = out_len*chans*2; | |
162 data->rate = af->data->rate; | |
163 return data; | |
164 } | |
165 | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22182
diff
changeset
|
166 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
|
167 af_resample_t *s = calloc(1,sizeof(af_resample_t)); |
13713 | 168 af->control=control; |
169 af->uninit=uninit; | |
170 af->play=play; | |
171 af->mul.n=1; | |
172 af->mul.d=1; | |
173 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
|
174 s->filter_length= 16; |
22755 | 175 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
|
176 s->phase_shift= 10; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
177 // 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
|
178 af->setup=s; |
13713 | 179 return AF_OK; |
180 } | |
181 | |
182 af_info_t af_info_lavcresample = { | |
183 "Sample frequency conversion using libavcodec", | |
184 "lavcresample", | |
185 "Michael Niedermayer", | |
186 "", | |
187 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
|
188 af_open |
13713 | 189 }; |