7568
|
1 /* Audio filter that adds and removes channels, according to the
|
|
2 command line parameter channels. It is stupid and can only add
|
|
3 silence or copy channels not mix or filter.
|
|
4 */
|
|
5 #include <stdio.h>
|
|
6 #include <stdlib.h>
|
|
7 #include <string.h>
|
7570
|
8 #include <unistd.h>
|
7568
|
9
|
|
10 #include "../config.h"
|
|
11 #include "../mp_msg.h"
|
|
12
|
|
13 #include "af.h"
|
|
14
|
|
15 // Local function for copying data
|
|
16 void copy(void* in, void* out, int ins, int inos,int outs, int outos, int len, int bps)
|
|
17 {
|
|
18 switch(bps){
|
|
19 case 1:{
|
|
20 int8_t* tin = (int8_t*)in;
|
|
21 int8_t* tout = (int8_t*)out;
|
|
22 tin += inos;
|
|
23 tout += outos;
|
|
24 len = len/ins;
|
|
25 while(len--){
|
|
26 *tout=*tin;
|
|
27 tin +=ins;
|
|
28 tout+=outs;
|
|
29 }
|
|
30 break;
|
|
31 }
|
|
32 case 2:{
|
|
33 int16_t* tin = (int16_t*)in;
|
|
34 int16_t* tout = (int16_t*)out;
|
|
35 tin += inos;
|
|
36 tout += outos;
|
|
37 len = len/(2*ins);
|
|
38 while(len--){
|
|
39 *tout=*tin;
|
|
40 tin +=ins;
|
|
41 tout+=outs;
|
|
42 }
|
|
43 break;
|
|
44 }
|
|
45 case 4:{
|
|
46 int32_t* tin = (int32_t*)in;
|
|
47 int32_t* tout = (int32_t*)out;
|
|
48 tin += inos;
|
|
49 tout += outos;
|
|
50 len = len/(4*ins);
|
|
51 while(len--){
|
|
52 *tout=*tin;
|
|
53 tin +=ins;
|
|
54 tout+=outs;
|
|
55 }
|
|
56 break;
|
|
57 }
|
|
58 case 8:{
|
|
59 int64_t* tin = (int64_t*)in;
|
|
60 int64_t* tout = (int64_t*)out;
|
|
61 tin += inos;
|
|
62 tout += outos;
|
|
63 len = len/(8*ins);
|
|
64 while(len--){
|
|
65 *tout=*tin;
|
|
66 tin +=ins;
|
|
67 tout+=outs;
|
|
68 }
|
|
69 break;
|
|
70 }
|
|
71 default:
|
|
72 mp_msg(MSGT_AFILTER,MSGL_ERR,"[channels] Unsupported number of bytes/sample: %i please report this error on the MPlayer mailing list. \n",bps);
|
|
73 }
|
|
74 }
|
|
75
|
|
76 // Initialization and runtime control
|
|
77 static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
78 {
|
|
79 switch(cmd){
|
|
80 case AF_CONTROL_REINIT:
|
|
81 // Make sure this filter isn't redundant
|
|
82 if(af->data->nch == ((af_data_t*)arg)->nch)
|
|
83 return AF_DETACH;
|
|
84
|
|
85 af->data->rate = ((af_data_t*)arg)->rate;
|
|
86 af->data->format = ((af_data_t*)arg)->format;
|
|
87 af->data->bps = ((af_data_t*)arg)->bps;
|
|
88 af->mul.n = af->data->nch;
|
|
89 af->mul.d = ((af_data_t*)arg)->nch;
|
|
90 return AF_OK;
|
|
91 case AF_CONTROL_CHANNELS:
|
|
92 // Reinit must be called after this function has been called
|
|
93
|
|
94 // Sanity check
|
|
95 if(((int*)arg)[0] <= 0 || ((int*)arg)[0] > 6){
|
|
96 mp_msg(MSGT_AFILTER,MSGL_ERR,"[channels] The number of output channels must be between 1 and 6. Current value is%i \n",((int*)arg)[0]);
|
|
97 return AF_ERROR;
|
|
98 }
|
|
99
|
|
100 af->data->nch=((int*)arg)[0];
|
|
101 mp_msg(MSGT_AFILTER,MSGL_V,"[channels] Changing number of channels to %i\n",af->data->nch);
|
|
102 return AF_OK;
|
|
103 }
|
|
104 return AF_UNKNOWN;
|
|
105 }
|
|
106
|
|
107 // Deallocate memory
|
|
108 static void uninit(struct af_instance_s* af)
|
|
109 {
|
|
110 if(af->data)
|
|
111 free(af->data);
|
|
112 }
|
|
113
|
|
114 // Filter data through filter
|
|
115 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
116 {
|
|
117 af_data_t* c = data; // Current working data
|
|
118 af_data_t* l = af->data; // Local data
|
|
119
|
|
120 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
121 return NULL;
|
|
122
|
|
123 // Reset unused channels if nch in < nch out
|
|
124 if(af->mul.n > af->mul.d)
|
7590
|
125 memset(l->audio,0,(c->len*af->mul.n)/af->mul.d);
|
7568
|
126
|
|
127 // Special case always output L & R
|
|
128 if(c->nch == 1){
|
|
129 copy(c->audio,l->audio,1,0,l->nch,0,c->len,c->bps);
|
|
130 copy(c->audio,l->audio,1,0,l->nch,1,c->len,c->bps);
|
|
131 }
|
|
132 else{
|
|
133 int i;
|
|
134 if(l->nch < c->nch){
|
|
135 for(i=0;i<l->nch;i++) // Truncates R if l->nch == 1 not good need mixing
|
|
136 copy(c->audio,l->audio,c->nch,i,l->nch,i,c->len,c->bps);
|
|
137 }
|
|
138 else{
|
|
139 for(i=0;i<c->nch;i++)
|
|
140 copy(c->audio,l->audio,c->nch,i,l->nch,i,c->len,c->bps);
|
|
141 }
|
|
142 }
|
|
143
|
|
144 // Set output data
|
|
145 c->audio = l->audio;
|
7590
|
146 c->len = (c->len*af->mul.n)/af->mul.d;
|
7568
|
147 c->nch = l->nch;
|
|
148
|
|
149 return c;
|
|
150 }
|
|
151
|
|
152 // Allocate memory and set function pointers
|
|
153 static int open(af_instance_t* af){
|
|
154 af->control=control;
|
|
155 af->uninit=uninit;
|
|
156 af->play=play;
|
|
157 af->mul.n=1;
|
|
158 af->mul.d=1;
|
|
159 af->data=calloc(1,sizeof(af_data_t));
|
|
160 if(af->data == NULL)
|
|
161 return AF_ERROR;
|
|
162 return AF_OK;
|
|
163 }
|
|
164
|
|
165 // Description of this filter
|
|
166 af_info_t af_info_channels = {
|
|
167 "Insert or remove channels",
|
|
168 "channels",
|
|
169 "Anders",
|
|
170 "",
|
7615
|
171 AF_FLAGS_REENTRANT,
|
7568
|
172 open
|
|
173 };
|