Mercurial > mplayer.hg
annotate libaf/af_lavcresample.c @ 19455:60c71a89cd76
Ouch, I mistakenly overwrote the contents from this file with a non-working
experimental version that was rotting in my local tree ...
author | diego |
---|---|
date | Sat, 19 Aug 2006 21:06:27 +0000 |
parents | 2408715522a7 |
children | c4d9550c9faf |
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 #define CHANS 6 | |
21 | |
13730 | 22 int64_t ff_gcd(int64_t a, int64_t b); |
23 | |
13713 | 24 // Data for specific instances of this filter |
25 typedef struct af_resample_s{ | |
26 struct AVResampleContext *avrctx; | |
27 int16_t *in[CHANS]; | |
28 int in_alloc; | |
29 int index; | |
30 | |
31 int filter_length; | |
32 int linear; | |
33 int phase_shift; | |
13730 | 34 double cutoff; |
13713 | 35 }af_resample_t; |
36 | |
37 | |
38 // Initialization and runtime control | |
39 static int control(struct af_instance_s* af, int cmd, void* arg) | |
40 { | |
41 af_resample_t* s = (af_resample_t*)af->setup; | |
42 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
|
43 int out_rate, test_output_res; // helpers for checking input format |
13713 | 44 |
45 switch(cmd){ | |
46 case AF_CONTROL_REINIT: | |
47 if((af->data->rate == data->rate) || (af->data->rate == 0)) | |
48 return AF_DETACH; | |
49 | |
50 af->data->nch = data->nch; | |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
51 if (af->data->nch > CHANS) af->data->nch = CHANS; |
14245 | 52 af->data->format = AF_FORMAT_S16_NE; |
13713 | 53 af->data->bps = 2; |
14433
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
54 af->mul.n = af->data->rate; |
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
55 af->mul.d = data->rate; |
95bb94a930a3
always cancel down fractions (frac_t) to avoid overflows and playback
reimar
parents:
14283
diff
changeset
|
56 af_frac_cancel(&af->mul); |
13730 | 57 af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate); |
13713 | 58 |
59 if(s->avrctx) av_resample_close(s->avrctx); | |
13730 | 60 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear, s->cutoff); |
13713 | 61 |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
62 // 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
|
63 out_rate = af->data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
64 af->data->rate = data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
65 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
|
66 af->data->rate = out_rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
67 return test_output_res; |
13713 | 68 case AF_CONTROL_COMMAND_LINE:{ |
13730 | 69 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); |
70 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80); | |
13713 | 71 return AF_OK; |
72 } | |
73 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
74 af->data->rate = *(int*)arg; | |
75 return AF_OK; | |
76 } | |
77 return AF_UNKNOWN; | |
78 } | |
79 | |
80 // Deallocate memory | |
81 static void uninit(struct af_instance_s* af) | |
82 { | |
83 if(af->data) | |
84 free(af->data); | |
85 if(af->setup){ | |
86 af_resample_t *s = af->setup; | |
87 if(s->avrctx) av_resample_close(s->avrctx); | |
88 free(s); | |
89 } | |
90 } | |
91 | |
92 // Filter data through filter | |
93 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
94 { | |
95 af_resample_t *s = af->setup; | |
96 int i, j, consumed, ret; | |
97 int16_t *in = (int16_t*)data->audio; | |
98 int16_t *out; | |
99 int chans = data->nch; | |
100 int in_len = data->len/(2*chans); | |
101 int out_len = (in_len*af->mul.n) / af->mul.d + 10; | |
102 int16_t tmp[CHANS][out_len]; | |
103 | |
104 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
105 return NULL; | |
106 | |
107 out= (int16_t*)af->data->audio; | |
13730 | 108 |
109 out_len= min(out_len, af->data->len/(2*chans)); | |
110 | |
13713 | 111 if(s->in_alloc < in_len + s->index){ |
112 s->in_alloc= in_len + s->index; | |
113 for(i=0; i<chans; i++){ | |
114 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); //FIXME free this maybe ;) | |
115 } | |
116 } | |
117 | |
14283 | 118 if(chans==1){ |
119 memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t)); | |
120 }else if(chans==2){ | |
121 for(j=0; j<in_len; j++){ | |
122 s->in[0][j + s->index]= *(in++); | |
123 s->in[1][j + s->index]= *(in++); | |
124 } | |
125 }else{ | |
126 for(j=0; j<in_len; j++){ | |
127 for(i=0; i<chans; i++){ | |
128 s->in[i][j + s->index]= *(in++); | |
129 } | |
13713 | 130 } |
131 } | |
132 in_len += s->index; | |
133 | |
134 for(i=0; i<chans; i++){ | |
135 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); | |
136 } | |
137 out_len= ret; | |
138 | |
139 s->index= in_len - consumed; | |
140 for(i=0; i<chans; i++){ | |
141 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t)); | |
142 } | |
143 | |
14283 | 144 if(chans==1){ |
145 memcpy(out, tmp[0], out_len*sizeof(int16_t)); | |
146 }else if(chans==2){ | |
147 for(j=0; j<out_len; j++){ | |
148 *(out++)= tmp[0][j]; | |
149 *(out++)= tmp[1][j]; | |
150 } | |
151 }else{ | |
152 for(j=0; j<out_len; j++){ | |
153 for(i=0; i<chans; i++){ | |
154 *(out++)= tmp[i][j]; | |
155 } | |
13713 | 156 } |
157 } | |
158 | |
159 data->audio = af->data->audio; | |
160 data->len = out_len*chans*2; | |
161 data->rate = af->data->rate; | |
162 return data; | |
163 } | |
164 | |
165 static int 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
|
166 af_resample_t *s = calloc(1,sizeof(af_resample_t)); |
13713 | 167 af->control=control; |
168 af->uninit=uninit; | |
169 af->play=play; | |
170 af->mul.n=1; | |
171 af->mul.d=1; | |
172 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
|
173 s->filter_length= 16; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
174 s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80); |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
175 s->phase_shift= 10; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
176 // 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
|
177 af->setup=s; |
13713 | 178 return AF_OK; |
179 } | |
180 | |
181 af_info_t af_info_lavcresample = { | |
182 "Sample frequency conversion using libavcodec", | |
183 "lavcresample", | |
184 "Michael Niedermayer", | |
185 "", | |
186 AF_FLAGS_REENTRANT, | |
187 open | |
188 }; |