annotate libaf/af_delay.c @ 8638:a2fa276b8ec7

The attached patch fixes a crash on *my* environment with certain DVD's. The sprintf function has been replaced by an snprintf function to prevent the buffer overflow. Erik Oomen <erik.oomen@home.nl>
author arpi
date Sun, 29 Dec 2002 15:34:50 +0000
parents d6f40a06867b
children 54c386615a70
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
1 /* This audio filter doesn't really do anything useful but serves an
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
2 example of how audio filters work. It delays the output signal by
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
3 the number of seconds set by delay=n where n is the number of
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
4 seconds.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
5 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
6 #include <stdio.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
7 #include <stdlib.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
8 #include <string.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
9
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
10 #include "af.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
11
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
12 // Data for specific instances of this filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
13 typedef struct af_delay_s
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
14 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
15 void* buf; // data block used for delaying audio signal
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
16 int len; // local buffer length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
17 float tlen; // Delay in seconds
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
18 }af_delay_t;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
19
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
20 // Initialization and runtime control
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
21 static int control(struct af_instance_s* af, int cmd, void* arg)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
22 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
23 switch(cmd){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
24 case AF_CONTROL_REINIT:{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
25 af->data->rate = ((af_data_t*)arg)->rate;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
26 af->data->nch = ((af_data_t*)arg)->nch;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
27 af->data->format = ((af_data_t*)arg)->format;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
28 af->data->bps = ((af_data_t*)arg)->bps;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
29
8607
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
30 return af->control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET,
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
31 &((af_delay_t*)af->setup)->tlen);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
32 }
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7745
diff changeset
33 case AF_CONTROL_COMMAND_LINE:{
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7745
diff changeset
34 float d = 0;
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7745
diff changeset
35 sscanf((char*)arg,"%f",&d);
8607
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
36 return af->control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET,&d);
7998
d48a06d07afb Adding commandline options for filters and fixing stupid bug in cfg
anders
parents: 7745
diff changeset
37 }
8607
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
38 case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
39 af_delay_t* s = (af_delay_t*)af->setup;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
40 void* bt = s->buf; // Old buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
41 int lt = s->len; // Old len
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
42
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
43 if(*((float*)arg) > 30 || *((float*)arg) < 0){
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
44 af_msg(AF_MSG_ERROR,"Error setting delay length in af_delay. Delay must be between 0s and 30s\n");
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
45 s->len=0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
46 s->tlen=0.0;
7665
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7615
diff changeset
47 af->delay=0.0;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
48 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
49 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
50
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
51 // Set new len and allocate new buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
52 s->tlen = *((float*)arg);
7665
fbd5445cc853 Adding function for calculating the delay caused by the filters
anders
parents: 7615
diff changeset
53 af->delay = s->tlen * 1000.0;
8607
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
54 s->len = af->data->rate*af->data->bps*af->data->nch*(int)s->tlen;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
55 s->buf = malloc(s->len);
8167
e8832e66babd New features:
anders
parents: 7998
diff changeset
56 af_msg(AF_MSG_DEBUG0,"[delay] Delaying audio output by %0.2fs\n",s->tlen);
e8832e66babd New features:
anders
parents: 7998
diff changeset
57 af_msg(AF_MSG_DEBUG1,"[delay] Delaying audio output by %i bytes\n",s->len);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
58
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
59 // Out of memory error
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
60 if(!s->buf){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
61 s->len = 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
62 free(bt);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
63 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
64 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
65
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
66 // Clear the new buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
67 memset(s->buf, 0, s->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
68
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
69 /* Copy old buffer to avoid click in output
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
70 sound (at least most of it) and release it */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
71 if(bt){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
72 memcpy(s->buf,bt,min(lt,s->len));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
73 free(bt);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
74 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
75 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
76 }
8607
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
77 case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
78 *((float*)arg) = ((af_delay_t*)af->setup)->tlen;
d6f40a06867b Changes includes:
anders
parents: 8348
diff changeset
79 return AF_OK;
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
80 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
81 return AF_UNKNOWN;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
82 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
83
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
84 // Deallocate memory
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
85 static void uninit(struct af_instance_s* af)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
86 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
87 if(af->data->audio)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
88 free(af->data->audio);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
89 if(af->data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
90 free(af->data);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
91 if(((af_delay_t*)(af->setup))->buf)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
92 free(((af_delay_t*)(af->setup))->buf);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
93 if(af->setup)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
94 free(af->setup);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
95 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
96
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
97 // Filter data through filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
98 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
99 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
100 af_data_t* c = data; // Current working data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
101 af_data_t* l = af->data; // Local data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
102 af_delay_t* s = (af_delay_t*)af->setup; // Setup for this instance
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
103
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
104
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
105 if(AF_OK != RESIZE_LOCAL_BUFFER(af , data))
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
106 return NULL;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
107
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
108 if(s->len > c->len){ // Delay bigger than buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
109 // Copy beginning of buffer to beginning of output buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
110 memcpy(l->audio,s->buf,c->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
111 // Move buffer left
7715
9774d0733dfa src and dest buffers may overlap
pl
parents: 7665
diff changeset
112 memmove(s->buf,s->buf+c->len,s->len-c->len);
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
113 // Save away current audio to end of buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
114 memcpy(s->buf+s->len-c->len,c->audio,c->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
115 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
116 else{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
117 // Copy end of previous block to beginning of output buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
118 memcpy(l->audio,s->buf,s->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
119 // Copy current block except end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
120 memcpy(l->audio+s->len,c->audio,c->len-s->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
121 // Save away end of current block for next call
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
122 memcpy(s->buf,c->audio+c->len-s->len,s->len);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
123 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
124
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
125 // Set output data
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
126 c->audio=l->audio;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
127
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
128 return c;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
129 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
130
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
131 // Allocate memory and set function pointers
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
132 static int open(af_instance_t* af){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
133 af->control=control;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
134 af->uninit=uninit;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
135 af->play=play;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
136 af->mul.n=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
137 af->mul.d=1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
138 af->data=calloc(1,sizeof(af_data_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
139 af->setup=calloc(1,sizeof(af_delay_t));
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
140 if(af->data == NULL || af->setup == NULL)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
141 return AF_ERROR;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
142 return AF_OK;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
143 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
144
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
145 // Description of this filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
146 af_info_t af_info_delay = {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
147 "Delay audio filter",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
148 "delay",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
149 "Anders",
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
150 "",
7615
c67328dd459a Adding Support for non-reentrant audio filters
anders
parents: 7568
diff changeset
151 AF_FLAGS_REENTRANT,
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
152 open
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
153 };
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
154
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
155