Mercurial > mplayer.hg
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 |
rev | line source |
---|---|
7568 | 1 /* This audio filter doesn't really do anything useful but serves an |
2 example of how audio filters work. It delays the output signal by | |
3 the number of seconds set by delay=n where n is the number of | |
4 seconds. | |
5 */ | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 | |
10 #include "af.h" | |
11 | |
12 // Data for specific instances of this filter | |
13 typedef struct af_delay_s | |
14 { | |
15 void* buf; // data block used for delaying audio signal | |
16 int len; // local buffer length | |
17 float tlen; // Delay in seconds | |
18 }af_delay_t; | |
19 | |
20 // Initialization and runtime control | |
21 static int control(struct af_instance_s* af, int cmd, void* arg) | |
22 { | |
23 switch(cmd){ | |
24 case AF_CONTROL_REINIT:{ | |
25 af->data->rate = ((af_data_t*)arg)->rate; | |
26 af->data->nch = ((af_data_t*)arg)->nch; | |
27 af->data->format = ((af_data_t*)arg)->format; | |
28 af->data->bps = ((af_data_t*)arg)->bps; | |
29 | |
8607 | 30 return af->control(af,AF_CONTROL_DELAY_LEN | AF_CONTROL_SET, |
31 &((af_delay_t*)af->setup)->tlen); | |
7568 | 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 | 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 | 38 case AF_CONTROL_DELAY_LEN | AF_CONTROL_SET:{ |
7568 | 39 af_delay_t* s = (af_delay_t*)af->setup; |
40 void* bt = s->buf; // Old buffer | |
41 int lt = s->len; // Old len | |
42 | |
43 if(*((float*)arg) > 30 || *((float*)arg) < 0){ | |
8167 | 44 af_msg(AF_MSG_ERROR,"Error setting delay length in af_delay. Delay must be between 0s and 30s\n"); |
7568 | 45 s->len=0; |
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 | 48 return AF_ERROR; |
49 } | |
50 | |
51 // Set new len and allocate new buffer | |
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 | 54 s->len = af->data->rate*af->data->bps*af->data->nch*(int)s->tlen; |
7568 | 55 s->buf = malloc(s->len); |
8167 | 56 af_msg(AF_MSG_DEBUG0,"[delay] Delaying audio output by %0.2fs\n",s->tlen); |
57 af_msg(AF_MSG_DEBUG1,"[delay] Delaying audio output by %i bytes\n",s->len); | |
7568 | 58 |
59 // Out of memory error | |
60 if(!s->buf){ | |
61 s->len = 0; | |
62 free(bt); | |
63 return AF_ERROR; | |
64 } | |
65 | |
66 // Clear the new buffer | |
67 memset(s->buf, 0, s->len); | |
68 | |
69 /* Copy old buffer to avoid click in output | |
70 sound (at least most of it) and release it */ | |
71 if(bt){ | |
72 memcpy(s->buf,bt,min(lt,s->len)); | |
73 free(bt); | |
74 } | |
75 return AF_OK; | |
76 } | |
8607 | 77 case AF_CONTROL_DELAY_LEN | AF_CONTROL_GET: |
78 *((float*)arg) = ((af_delay_t*)af->setup)->tlen; | |
79 return AF_OK; | |
7568 | 80 } |
81 return AF_UNKNOWN; | |
82 } | |
83 | |
84 // Deallocate memory | |
85 static void uninit(struct af_instance_s* af) | |
86 { | |
87 if(af->data->audio) | |
88 free(af->data->audio); | |
89 if(af->data) | |
90 free(af->data); | |
91 if(((af_delay_t*)(af->setup))->buf) | |
92 free(((af_delay_t*)(af->setup))->buf); | |
93 if(af->setup) | |
94 free(af->setup); | |
95 } | |
96 | |
97 // Filter data through filter | |
98 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
99 { | |
100 af_data_t* c = data; // Current working data | |
101 af_data_t* l = af->data; // Local data | |
102 af_delay_t* s = (af_delay_t*)af->setup; // Setup for this instance | |
103 | |
104 | |
105 if(AF_OK != RESIZE_LOCAL_BUFFER(af , data)) | |
106 return NULL; | |
107 | |
108 if(s->len > c->len){ // Delay bigger than buffer | |
109 // Copy beginning of buffer to beginning of output buffer | |
110 memcpy(l->audio,s->buf,c->len); | |
111 // Move buffer left | |
7715 | 112 memmove(s->buf,s->buf+c->len,s->len-c->len); |
7568 | 113 // Save away current audio to end of buffer |
114 memcpy(s->buf+s->len-c->len,c->audio,c->len); | |
115 } | |
116 else{ | |
117 // Copy end of previous block to beginning of output buffer | |
118 memcpy(l->audio,s->buf,s->len); | |
119 // Copy current block except end | |
120 memcpy(l->audio+s->len,c->audio,c->len-s->len); | |
121 // Save away end of current block for next call | |
122 memcpy(s->buf,c->audio+c->len-s->len,s->len); | |
123 } | |
124 | |
125 // Set output data | |
126 c->audio=l->audio; | |
127 | |
128 return c; | |
129 } | |
130 | |
131 // Allocate memory and set function pointers | |
132 static int open(af_instance_t* af){ | |
133 af->control=control; | |
134 af->uninit=uninit; | |
135 af->play=play; | |
136 af->mul.n=1; | |
137 af->mul.d=1; | |
138 af->data=calloc(1,sizeof(af_data_t)); | |
139 af->setup=calloc(1,sizeof(af_delay_t)); | |
140 if(af->data == NULL || af->setup == NULL) | |
141 return AF_ERROR; | |
142 return AF_OK; | |
143 } | |
144 | |
145 // Description of this filter | |
146 af_info_t af_info_delay = { | |
147 "Delay audio filter", | |
148 "delay", | |
149 "Anders", | |
150 "", | |
7615 | 151 AF_FLAGS_REENTRANT, |
7568 | 152 open |
153 }; | |
154 | |
155 |