annotate libaf/af_pan.c @ 10140:30cad6ad9dbc

fix segfaults with slices. support slice rendering into a filter even when the following filter/vo doesn't support slices. also use unified vf->dmpi rather than having vf->priv->dmpi duplicated in every filter.
author rfelker
date Tue, 20 May 2003 17:42:33 +0000
parents 93212da0032e
children 8631b43c9d82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
1 /*=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
2 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
3 // This software has been released under the terms of the GNU Public
d6f40a06867b Changes includes:
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d6f40a06867b Changes includes:
anders
parents:
diff changeset
5 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
6 // Copyright 2002 Anders Johansson ajh@atri.curtin.edu.au
d6f40a06867b Changes includes:
anders
parents:
diff changeset
7 //
d6f40a06867b Changes includes:
anders
parents:
diff changeset
8 //=============================================================================
d6f40a06867b Changes includes:
anders
parents:
diff changeset
9 */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
10
d6f40a06867b Changes includes:
anders
parents:
diff changeset
11 /* */
d6f40a06867b Changes includes:
anders
parents:
diff changeset
12
d6f40a06867b Changes includes:
anders
parents:
diff changeset
13 #include <stdio.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
14 #include <stdlib.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
15
d6f40a06867b Changes includes:
anders
parents:
diff changeset
16 #include <unistd.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
17 #include <inttypes.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
18 #include <math.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
19 #include <limits.h>
d6f40a06867b Changes includes:
anders
parents:
diff changeset
20
d6f40a06867b Changes includes:
anders
parents:
diff changeset
21 #include "af.h"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
22
d6f40a06867b Changes includes:
anders
parents:
diff changeset
23 // Data for specific instances of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
24 typedef struct af_pan_s
d6f40a06867b Changes includes:
anders
parents:
diff changeset
25 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
26 float level[AF_NCH][AF_NCH]; // Gain level for each channel
d6f40a06867b Changes includes:
anders
parents:
diff changeset
27 }af_pan_t;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
28
d6f40a06867b Changes includes:
anders
parents:
diff changeset
29 // Initialization and runtime control
d6f40a06867b Changes includes:
anders
parents:
diff changeset
30 static int control(struct af_instance_s* af, int cmd, void* arg)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
31 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
32 af_pan_t* s = af->setup;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
33
d6f40a06867b Changes includes:
anders
parents:
diff changeset
34 switch(cmd){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
35 case AF_CONTROL_REINIT:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
36 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
37 if(!arg) return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
38
d6f40a06867b Changes includes:
anders
parents:
diff changeset
39 af->data->rate = ((af_data_t*)arg)->rate;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
40 af->data->format = AF_FORMAT_F | AF_FORMAT_NE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
41 af->data->bps = 4;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
42 af->mul.n = af->data->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
43 af->mul.d = ((af_data_t*)arg)->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
44
d6f40a06867b Changes includes:
anders
parents:
diff changeset
45 if((af->data->format != ((af_data_t*)arg)->format) ||
d6f40a06867b Changes includes:
anders
parents:
diff changeset
46 (af->data->bps != ((af_data_t*)arg)->bps)){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
47 ((af_data_t*)arg)->format = af->data->format;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
48 ((af_data_t*)arg)->bps = af->data->bps;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
49 return AF_FALSE;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
50 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
51 return control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &af->data->nch);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
52 case AF_CONTROL_COMMAND_LINE:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
53 int nch = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
54 int n = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
55 char* cp = NULL;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
56 int j,k;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
57 // Read number of outputs
d6f40a06867b Changes includes:
anders
parents:
diff changeset
58 sscanf((char*)arg,"%i%n", &nch,&n);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
59 if(AF_OK != control(af,AF_CONTROL_PAN_NOUT | AF_CONTROL_SET, &nch))
d6f40a06867b Changes includes:
anders
parents:
diff changeset
60 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
61
d6f40a06867b Changes includes:
anders
parents:
diff changeset
62 // Read pan values
d6f40a06867b Changes includes:
anders
parents:
diff changeset
63 cp = &((char*)arg)[n];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
64 j = 0; k = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
65 while((*cp == ':') && (k < AF_NCH)){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
66 sscanf(cp, ":%f%n" , &s->level[k][j], &n);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
67 s->level[k][j] = clamp(s->level[k][j],0.0,1.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
68 af_msg(AF_MSG_VERBOSE,"[pan] Pan level from channel %i to"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
69 " channel %i = %f\n",j,k,s->level[k][j]);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
70 cp =&cp[n];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
71 j++;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
72 if(j>=nch){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
73 j = 0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
74 k++;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
75 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
76 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
77 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
78 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
79 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_SET:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
80 int i;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
81 int ch = ((af_control_ext_t*)arg)->ch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
82 float* level = ((af_control_ext_t*)arg)->arg;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
83 for(i=0;i<AF_NCH;i++)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
84 s->level[ch][i] = clamp(level[i],0.0,1.0);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
85 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
86 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
87 case AF_CONTROL_PAN_LEVEL | AF_CONTROL_GET:{
d6f40a06867b Changes includes:
anders
parents:
diff changeset
88 int i;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
89 int ch = ((af_control_ext_t*)arg)->ch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
90 float* level = ((af_control_ext_t*)arg)->arg;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
91 for(i=0;i<AF_NCH;i++)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
92 level[i] = s->level[ch][i];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
93 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
94 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
95 case AF_CONTROL_PAN_NOUT | AF_CONTROL_SET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
96 // Reinit must be called after this function has been called
d6f40a06867b Changes includes:
anders
parents:
diff changeset
97
d6f40a06867b Changes includes:
anders
parents:
diff changeset
98 // Sanity check
d6f40a06867b Changes includes:
anders
parents:
diff changeset
99 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > 6){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
100 af_msg(AF_MSG_ERROR,"[pan] The number of output channels must be"
d6f40a06867b Changes includes:
anders
parents:
diff changeset
101 " between 1 and %i. Current value is %i\n",AF_NCH,((int*)arg)[0]);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
102 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
103 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
104 af->data->nch=((int*)arg)[0];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
105 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
106 case AF_CONTROL_PAN_NOUT | AF_CONTROL_GET:
d6f40a06867b Changes includes:
anders
parents:
diff changeset
107 *(int*)arg = af->data->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
108 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
109 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
110 return AF_UNKNOWN;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
111 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
112
d6f40a06867b Changes includes:
anders
parents:
diff changeset
113 // Deallocate memory
d6f40a06867b Changes includes:
anders
parents:
diff changeset
114 static void uninit(struct af_instance_s* af)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
115 {
8674
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8607
diff changeset
116 if(af->data->audio)
93212da0032e 10l memory leak + bug fixes in ms to sample time conversion
anders
parents: 8607
diff changeset
117 free(af->data->audio);
8607
d6f40a06867b Changes includes:
anders
parents:
diff changeset
118 if(af->data)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
119 free(af->data);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
120 if(af->setup)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
121 free(af->setup);
d6f40a06867b Changes includes:
anders
parents:
diff changeset
122 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
123
d6f40a06867b Changes includes:
anders
parents:
diff changeset
124 // Filter data through filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
125 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
126 {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
127 af_data_t* c = data; // Current working data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
128 af_data_t* l = af->data; // Local data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
129 af_pan_t* s = af->setup; // Setup for this instance
d6f40a06867b Changes includes:
anders
parents:
diff changeset
130 float* in = c->audio; // Input audio data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
131 float* out = NULL; // Output audio data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
132 float* end = in+c->len/4; // End of loop
d6f40a06867b Changes includes:
anders
parents:
diff changeset
133 int nchi = c->nch; // Number of input channels
d6f40a06867b Changes includes:
anders
parents:
diff changeset
134 int ncho = l->nch; // Number of output channels
d6f40a06867b Changes includes:
anders
parents:
diff changeset
135 register int j,k;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
136
d6f40a06867b Changes includes:
anders
parents:
diff changeset
137 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
d6f40a06867b Changes includes:
anders
parents:
diff changeset
138 return NULL;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
139
d6f40a06867b Changes includes:
anders
parents:
diff changeset
140 out = l->audio;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
141 // Execute panning
d6f40a06867b Changes includes:
anders
parents:
diff changeset
142 // FIXME: Too slow
d6f40a06867b Changes includes:
anders
parents:
diff changeset
143 while(in < end){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
144 for(j=0;j<ncho;j++){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
145 register float x = 0.0;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
146 register float* tin = in;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
147 for(k=0;k<nchi;k++)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
148 x += tin[k] * s->level[j][k];
d6f40a06867b Changes includes:
anders
parents:
diff changeset
149 out[j] = x;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
150 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
151 out+= ncho;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
152 in+= nchi;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
153 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
154
d6f40a06867b Changes includes:
anders
parents:
diff changeset
155 // Set output data
d6f40a06867b Changes includes:
anders
parents:
diff changeset
156 c->audio = l->audio;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
157 c->len = (c->len*af->mul.n)/af->mul.d;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
158 c->nch = l->nch;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
159
d6f40a06867b Changes includes:
anders
parents:
diff changeset
160 return c;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
161 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
162
d6f40a06867b Changes includes:
anders
parents:
diff changeset
163 // Allocate memory and set function pointers
d6f40a06867b Changes includes:
anders
parents:
diff changeset
164 static int open(af_instance_t* af){
d6f40a06867b Changes includes:
anders
parents:
diff changeset
165 af->control=control;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
166 af->uninit=uninit;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
167 af->play=play;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
168 af->mul.n=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
169 af->mul.d=1;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
170 af->data=calloc(1,sizeof(af_data_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
171 af->setup=calloc(1,sizeof(af_pan_t));
d6f40a06867b Changes includes:
anders
parents:
diff changeset
172 if(af->data == NULL || af->setup == NULL)
d6f40a06867b Changes includes:
anders
parents:
diff changeset
173 return AF_ERROR;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
174 // Set initial pan to pass-through.
d6f40a06867b Changes includes:
anders
parents:
diff changeset
175 return AF_OK;
d6f40a06867b Changes includes:
anders
parents:
diff changeset
176 }
d6f40a06867b Changes includes:
anders
parents:
diff changeset
177
d6f40a06867b Changes includes:
anders
parents:
diff changeset
178 // Description of this filter
d6f40a06867b Changes includes:
anders
parents:
diff changeset
179 af_info_t af_info_pan = {
d6f40a06867b Changes includes:
anders
parents:
diff changeset
180 "Panning audio filter",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
181 "pan",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
182 "Anders",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
183 "",
d6f40a06867b Changes includes:
anders
parents:
diff changeset
184 AF_FLAGS_NOT_REENTRANT,
d6f40a06867b Changes includes:
anders
parents:
diff changeset
185 open
d6f40a06867b Changes includes:
anders
parents:
diff changeset
186 };