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