Mercurial > mplayer.hg
annotate libaf/af_lavcresample.c @ 14022:803be9a78854
removing strange csp matching code (was copy&pasted from vf_pp where it originated from arpi 2.5 years ago) -> fixes spp+scale+x11 crash
dont disable the filter by default (100l for iive)
author | michael |
---|---|
date | Tue, 23 Nov 2004 21:21:54 +0000 |
parents | 1b0d5a6ab7dc |
children | 5053490906c3 |
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; | |
46 | |
47 switch(cmd){ | |
48 case AF_CONTROL_REINIT: | |
49 if((af->data->rate == data->rate) || (af->data->rate == 0)) | |
50 return AF_DETACH; | |
51 | |
52 if(data->format != (AF_FORMAT_SI | AF_FORMAT_NE) || data->nch > CHANS) | |
53 return AF_ERROR; | |
54 | |
55 af->data->nch = data->nch; | |
56 af->data->format = AF_FORMAT_SI | AF_FORMAT_NE; | |
57 af->data->bps = 2; | |
13730 | 58 g= ff_gcd(af->data->rate, data->rate); |
59 af->mul.n = af->data->rate/g; | |
60 af->mul.d = data->rate/g; | |
61 af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate); | |
13713 | 62 |
63 if(s->avrctx) av_resample_close(s->avrctx); | |
13730 | 64 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear, s->cutoff); |
13713 | 65 |
66 return AF_OK; | |
67 case AF_CONTROL_COMMAND_LINE:{ | |
13730 | 68 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); |
69 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80); | |
13713 | 70 return AF_OK; |
71 } | |
72 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
73 af->data->rate = *(int*)arg; | |
74 return AF_OK; | |
75 } | |
76 return AF_UNKNOWN; | |
77 } | |
78 | |
79 // Deallocate memory | |
80 static void uninit(struct af_instance_s* af) | |
81 { | |
82 if(af->data) | |
83 free(af->data); | |
84 if(af->setup){ | |
85 af_resample_t *s = af->setup; | |
86 if(s->avrctx) av_resample_close(s->avrctx); | |
87 free(s); | |
88 } | |
89 } | |
90 | |
91 // Filter data through filter | |
92 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
93 { | |
94 af_resample_t *s = af->setup; | |
95 int i, j, consumed, ret; | |
96 int16_t *in = (int16_t*)data->audio; | |
97 int16_t *out; | |
98 int chans = data->nch; | |
99 int in_len = data->len/(2*chans); | |
100 int out_len = (in_len*af->mul.n) / af->mul.d + 10; | |
101 int16_t tmp[CHANS][out_len]; | |
102 | |
103 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data)) | |
104 return NULL; | |
105 | |
106 out= (int16_t*)af->data->audio; | |
13730 | 107 |
108 out_len= min(out_len, af->data->len/(2*chans)); | |
109 | |
13713 | 110 if(s->in_alloc < in_len + s->index){ |
111 s->in_alloc= in_len + s->index; | |
112 for(i=0; i<chans; i++){ | |
113 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); //FIXME free this maybe ;) | |
114 } | |
115 } | |
116 | |
117 for(j=0; j<in_len; j++){ | |
118 for(i=0; i<chans; i++){ | |
119 s->in[i][j + s->index]= *(in++); | |
120 } | |
121 } | |
122 in_len += s->index; | |
123 | |
124 for(i=0; i<chans; i++){ | |
125 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); | |
126 } | |
127 out_len= ret; | |
128 | |
129 s->index= in_len - consumed; | |
130 for(i=0; i<chans; i++){ | |
131 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t)); | |
132 } | |
133 | |
134 for(j=0; j<out_len; j++){ | |
135 for(i=0; i<chans; i++){ | |
136 *(out++)= tmp[i][j]; | |
137 } | |
138 } | |
139 | |
140 data->audio = af->data->audio; | |
141 data->len = out_len*chans*2; | |
142 data->rate = af->data->rate; | |
143 return data; | |
144 } | |
145 | |
146 static int open(af_instance_t* af){ | |
147 af->control=control; | |
148 af->uninit=uninit; | |
149 af->play=play; | |
150 af->mul.n=1; | |
151 af->mul.d=1; | |
152 af->data=calloc(1,sizeof(af_data_t)); | |
153 af->setup=calloc(1,sizeof(af_resample_t)); | |
154 ((af_resample_t*)af->setup)->filter_length= 16; | |
155 ((af_resample_t*)af->setup)->phase_shift= 10; | |
156 // ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY; | |
157 return AF_OK; | |
158 } | |
159 | |
160 af_info_t af_info_lavcresample = { | |
161 "Sample frequency conversion using libavcodec", | |
162 "lavcresample", | |
163 "Michael Niedermayer", | |
164 "", | |
165 AF_FLAGS_REENTRANT, | |
166 open | |
167 }; | |
168 #endif |