annotate libaf/af_channels.c @ 8007:87c979e641cb

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