456
615
target_buffer[i+1] = source_buffer[i];
619
/* the channel locations we know right now. code below assumes U is in slot 0, X in 1, M in 2 */
620
static char *mnemonics[]={
623
"L","C","R","CL","CR","SL","SR","BL","BC","BR","LFE",
624
"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10",
625
"A11","A12","A13","A14","A15","A16","A17","A18","A19","A20",
626
"A21","A22","A23","A24","A25","A26","A27","A28","A29","A30",
630
/* Check the requested matrix string for syntax and mnemonics */
631
static char *_sanitize_matrix(int maxchannels, char *matrix, ao_device *device){
633
char *ret = calloc(strlen(matrix)+1,1); /* can only get smaller */
636
while(count<maxchannels){
640
/* trim leading space */
641
while(*p && isspace(*p))p++;
643
/* search for seperator */
645
while(*h && *h!=',')h++;
647
/* trim trailing space */
649
while(t>p && isspace(*(t-1)))t--;
652
if(t-p && !strncmp(mnemonics[m],p,t-p) &&
653
strlen(mnemonics[m])==t-p){
654
if(count)strcat(ret,",");
655
strcat(ret,mnemonics[m]);
661
/* unrecognized channel mnemonic */
664
aerror("Unrecognized channel name \"");
665
for(i=0;i<t-p;i++)fputc(p[i],stderr);
666
fprintf(stderr,"\" in channel matrix \"%s\"\n",matrix);
680
static int _find_channel(int needle, char *haystack){
684
/* X does not map to anything, including X! */
685
if(needle==0) return -1;
691
/* search for seperator */
693
while(*h && *h!=',')h++;
696
if(!strncmp(mnemonics[needle],p,h-p) &&
697
strlen(mnemonics[needle])==h-p)break;
709
static char **_tokenize_matrix(char *matrix){
716
/* trim leading space */
717
while(*p && isspace(*p))p++;
719
/* search for seperator */
721
while(*h && *h!=',')h++;
723
/* trim trailing space */
725
while(t>p && isspace(*(t-1)))t--;
732
ret = calloc(count+1,sizeof(*ret));
739
/* trim leading space */
740
while(*p && isspace(*p))p++;
742
/* search for seperator */
744
while(*h && *h!=',')h++;
746
/* trim trailing space */
748
while(t>p && isspace(*(t-1)))t--;
750
ret[count] = calloc(t-p+1,1);
751
memcpy(ret[count],p,t-p);
761
static void _free_map(char **m){
770
static unsigned int _matrix_to_channelmask(int ch, char *matrix, char *premap, int **mout){
773
int *perm=(*mout=malloc(ch*sizeof(*mout)));
775
char **map = _tokenize_matrix(premap);
777
for(i=0;i<ch;i++) perm[i] = -1;
784
/* search for seperator */
785
while(*h && *h!=',')h++;
788
if(h-p && !strncmp(map[m],p,h-p) &&
793
/* X is a placeholder, X does not map to X */
794
if(map[m] && strcmp(map[m],"X")){
807
static char *_channelmask_to_matrix(unsigned int mask, char *premap){
810
char buffer[257]={0};
811
char **map = _tokenize_matrix(premap);
817
strcat(buffer,map[m]);
823
return strdup(buffer);
826
static int _channelmask_bits(unsigned int mask){
835
static int _channelmask_maxbit(unsigned int mask){
846
static char *_matrix_intersect(char *matrix,char *premap){
848
char buffer[257]={0};
850
char **map = _tokenize_matrix(premap);
856
/* search for seperator */
857
while(*h && *h!=',')h++;
860
if(h-p && !strncmp(map[m],p,h-p) &&
865
/* X is a placeholder, X does not map to X */
866
if(map[m] && strcmp(map[m],"X")){
869
strcat(buffer,map[m]);
878
return strdup(buffer);
881
static int ao_global_load_options(ao_option *options){
882
while (options != NULL) {
883
if(!strcmp(options->key,"debug")){
884
ao_global_dummy->verbose=2;
885
}else if(!strcmp(options->key,"verbose")){
886
if(ao_global_dummy->verbose<1)ao_global_dummy->verbose=1;
887
}else if(!strcmp(options->key,"quiet")){
888
ao_global_dummy->verbose=-1;
891
options = options->next;
898
static int ao_device_load_options(ao_device *device, ao_option *options){
900
while (options != NULL) {
901
if(!strcmp(options->key,"matrix")){
902
/* If a driver has a static channel mapping mechanism
903
(physically constant channel mapping, or at least an
904
unvarying set of constants for mapping channels), the
905
output_matrix is already set. An app/user specified
906
output mapping trumps. */
907
if(device->output_matrix)
908
free(device->output_matrix);
909
/* explicitly set the output matrix to the requested
910
string; devices must not override. */
911
device->output_matrix = _sanitize_matrix(32, options->value, device);
912
if(!device->output_matrix){
913
aerror("Empty or inavlid output matrix\n");
914
return AO_EBADOPTION;
916
adebug("Sanitized device output matrix: %s\n",device->output_matrix);
917
}else if(!strcmp(options->key,"debug")){
919
}else if(!strcmp(options->key,"verbose")){
920
if(device->verbose<1)device->verbose=1;
921
}else if(!strcmp(options->key,"quiet")){
924
if (!device->funcs->set_option(device, options->key, options->value)) {
925
/* Problem setting options */
926
return AO_EOPENDEVICE;
930
options = options->next;
461
936
/* Open a device. If this is a live device, file == NULL. */
462
static ao_device* _open_device(int driver_id, ao_sample_format *format,
937
static ao_device* _open_device(int driver_id, ao_sample_format *format,
463
938
ao_option *options, FILE *file)
465
940
ao_functions *funcs;
466
941
driver_list *driver;
942
ao_device *device=NULL;
944
ao_sample_format sformat=*format;
470
947
/* Get driver id */
471
948
if ( (driver = _get_driver(driver_id)) == NULL ) {
472
949
errno = AO_ENODRIVER;
473
return NULL; /* No driver exists */
476
953
funcs = driver->functions;
478
955
/* Check the driver type */
480
957
funcs->driver_info()->type != AO_TYPE_LIVE) {
482
959
errno = AO_ENOTLIVE;
484
} else if (file != NULL &&
961
} else if (file != NULL &&
485
962
funcs->driver_info()->type != AO_TYPE_FILE) {
487
964
errno = AO_ENOTFILE;
491
968
/* Make a new device structure */
492
if ( (device = _create_device(driver_id, driver,
969
if ( (device = _create_device(driver_id, driver,
493
970
format, file)) == NULL ) {
494
971
errno = AO_EFAIL;
495
return NULL; /* Couldn't alloc device */
498
/* Initialize the device memory */
975
/* Initialize the device memory; get static channel mapping */
499
976
if (!funcs->device_init(device)) {
501
977
errno = AO_EFAIL;
502
return NULL; /* Couldn't init internal memory */
505
981
/* Load options */
506
while (options != NULL) {
507
if (!funcs->set_option(device, options->key, options->value)) {
508
/* Problem setting options */
510
errno = AO_EOPENDEVICE;
514
options = options->next;
982
errno = ao_device_load_options(device,ao_global_options);
983
if(errno) goto error;
984
errno = ao_device_load_options(device,options);
985
if(errno) goto error;
987
/* also sanitize the format input channel matrix */
989
sformat.matrix = _sanitize_matrix(format->channels, format->matrix, device);
991
awarn("Input channel matrix invalid; ignoring.\n");
993
/* special-case handling of 'M'. */
994
if(sformat.channels==1 && sformat.matrix && !strcmp(sformat.matrix,"M")){
995
free(sformat.matrix);
1000
/* If device init was able to declare a static channel mapping
1001
mechanism, reconcile it to the input now. Odd drivers that
1002
need to communicate with a backend device to determine
1003
channel mapping strategy can still bypass this mechanism
1004
entirely. Also, drivers like ALSA may need to adjust
1005
strategy depending on what device is successfully opened,
1006
etc, but this still saves work later. */
1008
if(device->output_matrix && sformat.matrix){
1009
switch(device->output_matrix_order){
1010
case AO_OUTPUT_MATRIX_FIXED:
1011
/* Backend channel ordering is immutable and unused
1012
channels must still be sent. Look for the highest
1013
channel number we are able to map from the input
1016
unsigned int mask = _matrix_to_channelmask(sformat.channels,sformat.matrix,
1017
device->output_matrix,
1018
&device->input_map);
1019
int max = _channelmask_maxbit(mask);
1021
aerror("Unable to map any channels from input matrix to output");
1022
errno = AO_EBADFORMAT;
1025
device->output_channels = max+1;
1026
device->output_mask = mask;
1027
device->inter_matrix = strdup(device->output_matrix);
1031
case AO_OUTPUT_MATRIX_COLLAPSIBLE:
1032
/* the ordering of channels submitted to the backend is
1033
fixed, but only the channels in use should be present
1034
in the audio stream */
1036
unsigned int mask = _matrix_to_channelmask(sformat.channels,sformat.matrix,
1037
device->output_matrix,
1038
&device->input_map);
1039
int channels = _channelmask_bits(mask);
1041
aerror("Unable to map any channels from input matrix to output");
1042
errno = AO_EBADFORMAT;
1045
device->output_channels = channels;
1046
device->output_mask = mask;
1047
device->inter_matrix = _channelmask_to_matrix(mask,device->output_matrix);
1051
case AO_OUTPUT_MATRIX_PERMUTABLE:
1052
/* The ordering of channels is freeform. Only the
1053
channels in use should be sent, and they may be sent in
1054
any order. If possible, leave the input ordering
1057
unsigned int mask = _matrix_to_channelmask(sformat.channels,sformat.matrix,
1058
device->output_matrix,
1059
&device->input_map);
1060
int channels = _channelmask_bits(mask);
1062
aerror("Unable to map any channels from input matrix to output");
1063
errno = AO_EBADFORMAT;
1066
device->output_channels = channels;
1067
device->output_mask = mask;
1068
device->inter_matrix = _matrix_intersect(sformat.matrix,device->output_matrix);
1073
aerror("Driver backend failed to set output ordering.\n");
1079
device->output_channels = sformat.channels;
1082
/* other housekeeping */
1083
device->input_channels = sformat.channels;
1084
device->bytewidth = (sformat.bits+7)>>3;
1085
device->rate = sformat.rate;
517
1087
/* Open the device */
518
result = funcs->open(device, format);
1088
result = funcs->open(device, &sformat);
520
funcs->device_clear(device);
522
errno = AO_EOPENDEVICE;
523
return NULL; /* Couldn't open device */
1090
errno = AO_EOPENDEVICE;
1094
/* set up permutation based on finalized inter_matrix mapping */
1095
/* The only way device->output_channels could be zero here is
1096
if the driver has opted to ouput no channels (eg, the null
1099
if(!device->inter_matrix){
1100
awarn("Driver %s does not support automatic channel mapping;\n"
1101
"\tRouting only L/R channels to output.\n\n",
1102
info_table[device->driver_id]->short_name);
1103
device->inter_matrix=strdup("L,R");
1107
/* walk thorugh the inter matrix, match channels */
1108
char *op=device->inter_matrix;
1110
device->inter_permute = calloc(device->output_channels,sizeof(int));
1114
while(count<device->output_channels){
1119
/* find mnemonic offset of inter channel */
1120
while(*h && *h!=',')h++;
1121
while(mnemonics[m]){
1122
if(!strncmp(mnemonics[m],op,h-op))
1128
/* find match in input if any */
1129
device->inter_permute[count] = _find_channel(m,sformat.matrix);
1130
if(device->inter_permute[count] == -1 && sformat.channels == 1){
1131
device->inter_permute[count] = _find_channel(1,sformat.matrix);
1135
device->inter_permute[count] = -1;
1137
/* display resulting mapping for now */
1138
if(device->inter_permute[count]>=0){
1139
adebug("input %d (%s)\t -> backend %d (%s)\n",
1140
device->inter_permute[count], mnemonics[mm],
1141
count,mnemonics[m]);
1143
adebug(" \t backend %d (%s)\n",
1144
count,mnemonics[m]);
1151
char **inch = _tokenize_matrix(sformat.matrix);
1154
for(j=0;j<sformat.channels;j++){
1155
for(i=0;i<device->output_channels;i++)
1156
if(device->inter_permute[i]==j)break;
1157
if(i==device->output_channels){
1158
adebug("input %d (%s)\t -> none\n",
1165
awarn("Some input channels are unmapped and will not be used.\n");
1172
/* if there's no actual permutation to do, release the permutation vector */
1173
if(device->inter_permute && device->output_channels == device->input_channels){
1175
for(i=0;i<device->output_channels;i++)
1176
if(device->inter_permute[i]!=i)break;
1177
if(i==device->output_channels){
1178
free(device->inter_permute);
1179
device->inter_permute=NULL;
526
1183
/* Resolve actual driver byte format */
527
device->driver_byte_format =
1184
device->driver_byte_format =
528
1185
_real_byte_format(device->driver_byte_format);
530
/* Only create swap buffer for 16 bit samples if needed */
531
if (format->bits == 16 &&
532
device->client_byte_format != device->driver_byte_format) {
535
"n\n\n\n-------------------------\n"
537
"device->client_byte_format:%d\n"
538
"device->driver_byte_format:%d\n"
539
"--------------------------\n",
540
ao_is_big_endian(),device->client_byte_format,device->driver_byte_format);
542
result = _realloc_swap_buffer(device, DEF_SWAP_BUF_SIZE);
546
device->funcs->close(device);
547
device->funcs->device_clear(device);
550
return NULL; /* Couldn't alloc swap buffer */
1187
/* Only create swap buffer if needed */
1188
if (device->bytewidth>1 &&
1189
device->client_byte_format != device->driver_byte_format){
1190
adebug("swap buffer required:\n");
1191
adebug(" machine endianness: %d\n",ao_is_big_endian());
1192
adebug(" device->client_byte_format:%d\n",device->client_byte_format);
1193
adebug(" device->driver_byte_format:%d\n",device->driver_byte_format);
1196
if ( (device->bytewidth>1 &&
1197
device->client_byte_format != device->driver_byte_format) ||
1198
device->inter_permute){
1200
result = _realloc_swap_buffer(device, DEF_SWAP_BUF_SIZE);
1204
if(sformat.matrix)free(sformat.matrix);
1205
device->funcs->close(device);
1206
device->funcs->device_clear(device);
1209
return NULL; /* Couldn't alloc swap buffer */
554
1213
/* If we made it this far, everything is OK. */
1214
if(sformat.matrix)free(sformat.matrix);
1219
int errtemp = errno;
1221
free(sformat.matrix);