vd_ffmpeg.c: Use FFmpeg-mt compatible code for codec delay
[mplayer.git] / libaf / af_delay.c
blob4b94e00066da3809ee3c09676e582b3ad7ebd8b1
1 /*
2 * This audio filter delays the output signal for the different
3 * channels and can be used for simple position panning.
4 * An extension for this filter would be a reverb.
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
28 #include "af.h"
30 #define L 65536
32 #define UPDATEQI(qi) qi=(qi+1)&(L-1)
34 // Data for specific instances of this filter
35 typedef struct af_delay_s
37 void* q[AF_NCH]; // Circular queues used for delaying audio signal
38 int wi[AF_NCH]; // Write index
39 int ri; // Read index
40 float d[AF_NCH]; // Delay [ms]
41 }af_delay_t;
43 // Initialization and runtime control
44 static int control(struct af_instance_s* af, int cmd, void* arg)
46 af_delay_t* s = af->setup;
47 switch(cmd){
48 case AF_CONTROL_REINIT:{
49 int i;
51 // Free prevous delay queues
52 for(i=0;i<af->data->nch;i++){
53 if(s->q[i])
54 free(s->q[i]);
57 af->data->rate = ((af_data_t*)arg)->rate;
58 af->data->nch = ((af_data_t*)arg)->nch;
59 af->data->format = ((af_data_t*)arg)->format;
60 af->data->bps = ((af_data_t*)arg)->bps;
62 // Allocate new delay queues
63 for(i=0;i<af->data->nch;i++){
64 s->q[i] = calloc(L,af->data->bps);
65 if(NULL == s->q[i])
66 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[delay] Out of memory\n");
69 return control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET,s->d);
71 case AF_CONTROL_COMMAND_LINE:{
72 int n = 1;
73 int i = 0;
74 char* cl = arg;
75 while(n && i < AF_NCH ){
76 sscanf(cl,"%f:%n",&s->d[i],&n);
77 if(n==0 || cl[n-1] == '\0')
78 break;
79 cl=&cl[n];
80 i++;
82 return AF_OK;
84 case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{
85 int i;
86 if(AF_OK != af_from_ms(AF_NCH, arg, s->wi, af->data->rate, 0.0, 1000.0))
87 return AF_ERROR;
88 s->ri = 0;
89 for(i=0;i<AF_NCH;i++){
90 mp_msg(MSGT_AFILTER, MSGL_DBG2, "[delay] Channel %i delayed by %0.3fms\n",
91 i,clamp(s->d[i],0.0,1000.0));
92 mp_msg(MSGT_AFILTER, MSGL_DBG3, "[delay] Channel %i delayed by %i samples\n",
93 i,s->wi[i]);
95 return AF_OK;
97 case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET:{
98 int i;
99 for(i=0;i<AF_NCH;i++){
100 if(s->ri > s->wi[i])
101 s->wi[i] = L - (s->ri - s->wi[i]);
102 else
103 s->wi[i] = s->wi[i] - s->ri;
105 return af_to_ms(AF_NCH, s->wi, arg, af->data->rate);
108 return AF_UNKNOWN;
111 // Deallocate memory
112 static void uninit(struct af_instance_s* af)
114 int i;
115 if(af->data)
116 free(af->data);
117 for(i=0;i<AF_NCH;i++)
118 if(((af_delay_t*)(af->setup))->q[i])
119 free(((af_delay_t*)(af->setup))->q[i]);
120 if(af->setup)
121 free(af->setup);
124 // Filter data through filter
125 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
127 af_data_t* c = data; // Current working data
128 af_delay_t* s = af->setup; // Setup for this instance
129 int nch = c->nch; // Number of channels
130 int len = c->len/c->bps; // Number of sample in data chunk
131 int ri = 0;
132 int ch,i;
133 for(ch=0;ch<nch;ch++){
134 switch(c->bps){
135 case 1:{
136 int8_t* a = c->audio;
137 int8_t* q = s->q[ch];
138 int wi = s->wi[ch];
139 ri = s->ri;
140 for(i=ch;i<len;i+=nch){
141 q[wi] = a[i];
142 a[i] = q[ri];
143 UPDATEQI(wi);
144 UPDATEQI(ri);
146 s->wi[ch] = wi;
147 break;
149 case 2:{
150 int16_t* a = c->audio;
151 int16_t* q = s->q[ch];
152 int wi = s->wi[ch];
153 ri = s->ri;
154 for(i=ch;i<len;i+=nch){
155 q[wi] = a[i];
156 a[i] = q[ri];
157 UPDATEQI(wi);
158 UPDATEQI(ri);
160 s->wi[ch] = wi;
161 break;
163 case 4:{
164 int32_t* a = c->audio;
165 int32_t* q = s->q[ch];
166 int wi = s->wi[ch];
167 ri = s->ri;
168 for(i=ch;i<len;i+=nch){
169 q[wi] = a[i];
170 a[i] = q[ri];
171 UPDATEQI(wi);
172 UPDATEQI(ri);
174 s->wi[ch] = wi;
175 break;
179 s->ri = ri;
180 return c;
183 // Allocate memory and set function pointers
184 static int af_open(af_instance_t* af){
185 af->control=control;
186 af->uninit=uninit;
187 af->play=play;
188 af->mul=1;
189 af->data=calloc(1,sizeof(af_data_t));
190 af->setup=calloc(1,sizeof(af_delay_t));
191 if(af->data == NULL || af->setup == NULL)
192 return AF_ERROR;
193 return AF_OK;
196 // Description of this filter
197 af_info_t af_info_delay = {
198 "Delay audio filter",
199 "delay",
200 "Anders",
202 AF_FLAGS_REENTRANT,
203 af_open