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