aboutsummaryrefslogtreecommitdiffhomepage
path: root/libao2/pl_format.c
blob: 93c3d701cd11f1e42fb0871b5c8aa6e6de409c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* This audio output plugin changes the format of a data block. Valid
   output formats are: AFMT_U8, AFMT_S8, AFMT_S16_LE, AFMT_S16_BE
   AFMT_U16_LE, AFMT_U16_BE, AFMT_S32_LE and AFMT_S32_BE. The output
   format is spedified using the cfg switch 'format=NR' where NR is
   the number as given in libao2/afmt.h
*/

#define PLUGIN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>

#include "audio_out.h"
#include "audio_plugin.h"
#include "audio_plugin_internal.h"
#include "afmt.h"

static ao_info_t info =
{
        "Sample format conversion audio plugin",
        "format",
        "Anders",
        ""
};

LIBAO_PLUGIN_EXTERN(format)

// local data
typedef struct pl_format_s
{
  void*  data;       // local audio data block
  int    len;        // local buffer length
  int 	 in;  	     // input fomat
  int    out;        // output fomat
  double sz_mult;    // data size multiplier
} pl_format_t;

static pl_format_t pl_format={NULL,0,0,0,1};

// Number of bits
#define B08		(0<<0) 
#define B16  		(1<<0)	
#define B32  		(2<<0)
#define NBITS_MASK	(3<<0)

// Endianess
#define BE 		(0<<2) // Big Endian
#define LE 		(1<<2) // Little Endian
#define END_MASK	(1<<2)

// Signed
#define US		(0<<3) // Un Signed
#define SI		(1<<3) // SIgned
#define SIGN_MASK	(1<<3)

// to set/get/query special features/parameters
static int control(int cmd,int arg){
  switch(cmd){
  case AOCONTROL_PLUGIN_SET_LEN:
    if(pl_format.data) 
      free(pl_format.data);
    pl_format.len = ao_plugin_data.len;
    pl_format.data=(void*)malloc(ao_plugin_data.len);
    if(!pl_format.data)
      return CONTROL_ERROR;
    ao_plugin_data.len=(int)(((double)ao_plugin_data.len)/pl_format.sz_mult);
    return CONTROL_OK;
  }
  return -1;
}

// open & setup audio device
// return: 1=success 0=fail
static int init(){
  // Sheck input format
  switch(ao_plugin_data.format){
  case(AFMT_U8):
    pl_format.in=LE|B08|US; break;
  case(AFMT_S8):
    pl_format.in=LE|B08|SI; break;
  case(AFMT_S16_LE):
    pl_format.in=LE|B16|SI; break;
  case(AFMT_S16_BE):
    pl_format.in=BE|B16|SI; break;
  case(AFMT_U16_LE):	
    pl_format.in=LE|B16|US; break;
  case(AFMT_U16_BE):	
    pl_format.in=BE|B16|US; break;
  case(AFMT_S32_LE):
    pl_format.in=LE|B32|SI; break;
  case(AFMT_S32_BE):	
    pl_format.in=BE|B32|SI; break;
  case(AFMT_IMA_ADPCM):		
  case(AFMT_MU_LAW):
  case(AFMT_A_LAW):
  case(AFMT_MPEG):
  case(AFMT_AC3):
    printf("[pl_format] Input audio format not yet suported \n");
    return 0;
  default: 
    printf("[pl_format] Unrecognised input audio format\n"); //This can not happen .... 
    return 0;
  }
  // Sheck output format
  switch(ao_plugin_cfg.pl_format_type){
  case(AFMT_U8):
    pl_format.out=LE|B08|US; break;
  case(AFMT_S8):
    pl_format.out=LE|B08|SI; break;
  case(AFMT_S16_LE):
    pl_format.out=LE|B16|SI; break;
  case(AFMT_S16_BE):
    pl_format.out=BE|B16|SI; break;
  case(AFMT_U16_LE):	
    pl_format.out=LE|B16|US; break;
  case(AFMT_U16_BE):	
    pl_format.out=BE|B16|US; break;
  case(AFMT_S32_LE):
    pl_format.out=LE|B32|SI; break;
  case(AFMT_S32_BE):	
    pl_format.out=BE|B32|SI; break;
  case(AFMT_IMA_ADPCM):		
  case(AFMT_MU_LAW):
  case(AFMT_A_LAW):
  case(AFMT_MPEG):
  case(AFMT_AC3):
    printf("[pl_format] Output audio format not yet suported \n");
    return 0;
  default:
    printf("[pl_format] Unrecognised audio output format\n");
    return 0;
  }

  // Tell the world what we are up to
  printf("[pl_format] Input format: %s, output format: %s \n",
	 audio_out_format_name(ao_plugin_data.format),
	 audio_out_format_name(ao_plugin_cfg.pl_format_type));

  // We are changing the format
  ao_plugin_data.format=ao_plugin_cfg.pl_format_type;

  // And perhaps the buffer size
  pl_format.sz_mult=1;
  if((pl_format.in&NBITS_MASK) > (pl_format.out&NBITS_MASK))
    pl_format.sz_mult/=(double)(1<<((pl_format.in&NBITS_MASK)-(pl_format.out&NBITS_MASK)));
  if((pl_format.in&NBITS_MASK) < (pl_format.out&NBITS_MASK))
    pl_format.sz_mult*=(double)(1<<((pl_format.out&NBITS_MASK)-(pl_format.in&NBITS_MASK)));
  ao_plugin_data.sz_mult/=pl_format.sz_mult;

  return 1;
}

// close plugin
static void uninit(){
  if(pl_format.data) 
    free(pl_format.data);
  pl_format.data=NULL;
}

// empty buffers
static void reset(){
  memset(pl_format.data, 0, pl_format.len);
}

// processes 'ao_plugin_data.len' bytes of 'data'
// called for every block of data
// FIXME: this routine needs to be optimized (it is probably possible to do a lot here)
static int play(){
  register int i=0;
  void* in_data=ao_plugin_data.data;
  void* out_data=pl_format.data;
  int len=(ao_plugin_data.len)>>(pl_format.in&NBITS_MASK);
  ao_plugin_data.len=(int)(((double)ao_plugin_data.len)*=pl_format.sz_mult);
  
  // Change to little endian (Is this true for sun ?)
  if((pl_format.in&END_MASK)!=LE){
    switch(pl_format.in&NBITS_MASK){
    case(B16):{
      register uint16_t s;
      for(i=1;i<len;i++){
	s=((uint16_t*)in_data)[i];
	((uint16_t*)in_data)[i]=(uint16_t)(((s&0x00FF)<<8) | (s&0xFF00)>>8);
      }
    }
    break;
    case(B32):{
      register uint32_t s;
      for(i=1;i<len;i++){
	s=((uint32_t*)in_data)[i];
	((uint32_t*)in_data)[i]=(uint32_t)(((s&0x000000FF)<<24) | ((s&0x0000FF00)<<8) |
					   ((s&0x00FF0000)>>8)  | ((s&0xFF000000)>>24));
      }
    }
    break;
    }
  }
  // Change signed/unsigned
  if((pl_format.in&SIGN_MASK) != (pl_format.out&SIGN_MASK)){
    switch((pl_format.in&NBITS_MASK)){
    case(B08):
      switch(pl_format.in&SIGN_MASK){
      case(US):
	for(i=0;i<len;i++)
	((int8_t*)in_data)[i]=(int8_t)(-127+((int)((uint8_t*)in_data)[i]));
	break;
      case(SI):
	for(i=0;i<len;i++)
	((uint8_t*)in_data)[i]=(uint8_t)(+128+((int)((int8_t*)in_data)[i]));
	break;
      }
      break;
    case(B16):
      switch(pl_format.in&SIGN_MASK){
      case(US):
	for(i=0;i<len;i++)
	  ((int16_t*)in_data)[i]=(int16_t)(-32767+((int)((uint16_t*)in_data)[i]));
	break;
      case(SI):
	for(i=0;i<len;i++)
	  ((uint16_t*)in_data)[i]=(uint16_t)(+32768+((int)((int16_t*)in_data)[i]));
	break;
      }
      break;
    case(B32):
      switch(pl_format.in&SIGN_MASK){
      case(US):
	for(i=0;i<len;i++)
	((int32_t*)in_data)[i]=(int32_t)(-(1<<31-1)+((uint32_t*)in_data)[i]);
	break;
      case(SI):
	for(i=0;i<len;i++)
	((uint32_t*)in_data)[i]=(uint32_t)(+(1<<31)+((int32_t*)in_data)[i]);
	break;
      }
      break;
    }	
  }
  // Change the number of bits
  if((pl_format.in&NBITS_MASK) == (pl_format.out&NBITS_MASK)){
    int sz=(int)((double)ao_plugin_data.len/pl_format.sz_mult);
    for(i=0;i<sz;i++)
      ((char*)out_data)[i]=((char*)in_data)[i];
  } else {
    switch(pl_format.in&NBITS_MASK){
    case(B08):
      switch(pl_format.out&NBITS_MASK){
      case(B16):
	for(i=1;i<len;i++)
	  ((uint16_t*)out_data)[i]=((uint16_t)((uint8_t*)in_data)[i])<<8;
	break;
      case(B32):
	for(i=1;i<len;i++)
	  ((uint32_t*)out_data)[i]=((uint32_t)((uint8_t*)in_data)[i])<<24;
	break;
      }
      break;
    case(B16):
      switch(pl_format.out&NBITS_MASK){
      case(B08):
	for(i=0;i<len;i++)
	  ((uint8_t*)out_data)[i]=(uint8_t)((((uint16_t*)in_data)[i])>>8);
	break;
      case(B32):
	for(i=1;i<len;i++)
	  ((uint32_t*)out_data)[i]=((uint32_t)((uint16_t*)in_data)[i])<<16;
	break;
      }
      break;
    case(B32):
      switch(pl_format.out&NBITS_MASK){
      case(B08):
	for(i=0;i<len;i++)
	  ((uint8_t*)out_data)[i]=(uint8_t)((((uint32_t*)in_data)[i])>>24);
	break;
      case(B16):
	for(i=1;i<len;i++)
	  ((uint16_t*)out_data)[i]=(uint16_t)((((uint32_t*)in_data)[i])>>16);
	break;
      }
      break;      
    }
  }
  // Switch to the correct endainess (agiain the problem with sun?)
  if((pl_format.out&END_MASK)!=LE){
    switch(pl_format.in&NBITS_MASK){
    case(B16):{
      register uint16_t s;
      for(i=1;i<len;i++){
	s=((uint16_t*)out_data)[i];
	((uint16_t*)out_data)[i]=(uint16_t)(((s&0x00FF)<<8) | (s&0xFF00)>>8);
      }
    }
    break;
    case(B32):{
      register uint32_t s;
      for(i=1;i<len;i++){
	s=((uint32_t*)out_data)[i];
	((uint32_t*)out_data)[i]=(uint32_t)(((s&0x000000FF)<<24) | ((s&0x0000FF00)<<8) |
					    ((s&0x00FF0000)>>8)  | ((s&0xFF000000)>>24));
      }
    }
    break;
    }
  }
  ao_plugin_data.data=out_data;
  return 1;
}