Mercurial > mplayer.hg
annotate libaf/af_lavcresample.c @ 24900:9079c9745ff9
A/V sync: take audio filter buffers into account
Substract the delay caused by filter buffering when calculating
currently playing audio position. This matters for af_scaletempo which
buffers significant and varying amounts of data. For other current
filters the effect is normally insignificant.
Instead of the old time-based filter delay field (which was ignored)
this version stores the per-filter delay in units of bytes input read
without corresponding output. This allows the current scaletempo
behavior where other filters before and after it can see the same
nominal samplerate even though the real duration of the data varies;
in this case the other filters can not know the delay they're causing
in terms of real time.
author | uau |
---|---|
date | Thu, 01 Nov 2007 06:52:50 +0000 |
parents | b2402b4f0afa |
children | 867ee1c2114b |
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; |
24888 | 50 af->mul = (double)af->data->rate / data->rate; |
24900 | 51 af->delay = af->data->nch * s->filter_length / min(af->mul, 1); // *bps*.5 |
13713 | 52 |
53 if(s->avrctx) av_resample_close(s->avrctx); | |
24888 | 54 s->avrctx= av_resample_init(af->data->rate, /*in_rate*/data->rate, s->filter_length, s->phase_shift, s->linear, s->cutoff); |
13713 | 55 |
14213
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
56 // 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
|
57 out_rate = af->data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
58 af->data->rate = data->rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
59 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
|
60 af->data->rate = out_rate; |
3c56b18bbb0c
Make filters request a supported input format instead of failing.
reimar
parents:
14186
diff
changeset
|
61 return test_output_res; |
13713 | 62 case AF_CONTROL_COMMAND_LINE:{ |
22755 | 63 s->cutoff= 0.0; |
13730 | 64 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff); |
22755 | 65 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80); |
13713 | 66 return AF_OK; |
67 } | |
68 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: | |
69 af->data->rate = *(int*)arg; | |
70 return AF_OK; | |
71 } | |
72 return AF_UNKNOWN; | |
73 } | |
74 | |
75 // Deallocate memory | |
76 static void uninit(struct af_instance_s* af) | |
77 { | |
78 if(af->data) | |
22179 | 79 free(af->data->audio); |
80 free(af->data); | |
13713 | 81 if(af->setup){ |
22182 | 82 int i; |
13713 | 83 af_resample_t *s = af->setup; |
84 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
|
85 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
|
86 free(s->in[i]); |
13713 | 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); | |
24888 | 100 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
|
101 int16_t tmp[AF_NCH][out_len]; |
13713 | 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++){ | |
22179 | 113 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); |
13713 | 114 } |
115 } | |
116 | |
14283 | 117 if(chans==1){ |
118 memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t)); | |
119 }else if(chans==2){ | |
120 for(j=0; j<in_len; j++){ | |
121 s->in[0][j + s->index]= *(in++); | |
122 s->in[1][j + s->index]= *(in++); | |
123 } | |
124 }else{ | |
125 for(j=0; j<in_len; j++){ | |
126 for(i=0; i<chans; i++){ | |
127 s->in[i][j + s->index]= *(in++); | |
128 } | |
13713 | 129 } |
130 } | |
131 in_len += s->index; | |
132 | |
133 for(i=0; i<chans; i++){ | |
134 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans); | |
135 } | |
136 out_len= ret; | |
137 | |
138 s->index= in_len - consumed; | |
139 for(i=0; i<chans; i++){ | |
140 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t)); | |
141 } | |
142 | |
14283 | 143 if(chans==1){ |
144 memcpy(out, tmp[0], out_len*sizeof(int16_t)); | |
145 }else if(chans==2){ | |
146 for(j=0; j<out_len; j++){ | |
147 *(out++)= tmp[0][j]; | |
148 *(out++)= tmp[1][j]; | |
149 } | |
150 }else{ | |
151 for(j=0; j<out_len; j++){ | |
152 for(i=0; i<chans; i++){ | |
153 *(out++)= tmp[i][j]; | |
154 } | |
13713 | 155 } |
156 } | |
157 | |
158 data->audio = af->data->audio; | |
159 data->len = out_len*chans*2; | |
160 data->rate = af->data->rate; | |
161 return data; | |
162 } | |
163 | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
22182
diff
changeset
|
164 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
|
165 af_resample_t *s = calloc(1,sizeof(af_resample_t)); |
13713 | 166 af->control=control; |
167 af->uninit=uninit; | |
168 af->play=play; | |
24888 | 169 af->mul=1; |
13713 | 170 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
|
171 s->filter_length= 16; |
22755 | 172 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
|
173 s->phase_shift= 10; |
5053490906c3
Initialize cutoff, too. Fixes crash when AF_CONTROL_COMMAND_LINE is not set.
reimar
parents:
13859
diff
changeset
|
174 // 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
|
175 af->setup=s; |
13713 | 176 return AF_OK; |
177 } | |
178 | |
179 af_info_t af_info_lavcresample = { | |
180 "Sample frequency conversion using libavcodec", | |
181 "lavcresample", | |
182 "Michael Niedermayer", | |
183 "", | |
184 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
|
185 af_open |
13713 | 186 }; |