Mercurial > mplayer.hg
annotate libao2/ao_sdl.c @ 7155:66019eb62edc
"halfpack" (yuv planar 4:2:0 -> packed 4:2:2, half height) video filter
(useful for downsampling luma for low-res output devices without
losing chroma samples, when hardware downscaling is poor quality or
unavailable)
author | rfelker |
---|---|
date | Fri, 30 Aug 2002 06:16:40 +0000 |
parents | 0380dfad2db9 |
children | 7ff053498bf2 |
rev | line source |
---|---|
972 | 1 /* |
2 * ao_sdl.c - libao2 SDLlib Audio Output Driver for MPlayer | |
3 * | |
4 * This driver is under the same license as MPlayer. | |
5 * (http://mplayer.sf.net) | |
6 * | |
7 * Copyleft 2001 by Felix Bünemann (atmosfear@users.sf.net) | |
8 * | |
9 * Thanks to Arpi for nice ringbuffer-code! | |
10 * | |
11 */ | |
12 | |
966 | 13 #include <stdio.h> |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
14 #include <stdlib.h> |
966 | 15 |
16 #include "audio_out.h" | |
17 #include "audio_out_internal.h" | |
1066 | 18 #include "afmt.h" |
6184 | 19 #include <SDL.h> |
966 | 20 |
972 | 21 #include "../libvo/fastmemcpy.h" |
22 | |
966 | 23 static ao_info_t info = |
24 { | |
25 "SDLlib audio output", | |
26 "sdl", | |
27 "Felix Buenemann <atmosfear@users.sourceforge.net>", | |
28 "" | |
29 }; | |
30 | |
31 LIBAO_EXTERN(sdl) | |
32 | |
33 | |
983 | 34 extern int verbose; |
35 | |
972 | 36 // Samplesize used by the SDLlib AudioSpec struct |
993
dfa641c44d4a
Increased sample size to solve sound problems one some systems.
atmosfear
parents:
985
diff
changeset
|
37 #define SAMPLESIZE 1024 |
972 | 38 |
39 // General purpose Ring-buffering routines | |
40 | |
974 | 41 #define BUFFSIZE 4096 |
1066 | 42 #define NUM_BUFS 16 |
972 | 43 |
44 static unsigned char *buffer[NUM_BUFS]; | |
45 | |
46 static unsigned int buf_read=0; | |
47 static unsigned int buf_write=0; | |
48 static unsigned int buf_read_pos=0; | |
49 static unsigned int buf_write_pos=0; | |
6184 | 50 static unsigned int volume=127; |
972 | 51 static int full_buffers=0; |
52 static int buffered_bytes=0; | |
53 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
54 |
972 | 55 static int write_buffer(unsigned char* data,int len){ |
56 int len2=0; | |
57 int x; | |
58 while(len>0){ | |
59 if(full_buffers==NUM_BUFS) break; | |
60 x=BUFFSIZE-buf_write_pos; | |
61 if(x>len) x=len; | |
62 memcpy(buffer[buf_write]+buf_write_pos,data+len2,x); | |
63 len2+=x; len-=x; | |
64 buffered_bytes+=x; buf_write_pos+=x; | |
65 if(buf_write_pos>=BUFFSIZE){ | |
66 // block is full, find next! | |
67 buf_write=(buf_write+1)%NUM_BUFS; | |
68 ++full_buffers; | |
69 buf_write_pos=0; | |
70 } | |
71 } | |
72 return len2; | |
73 } | |
74 | |
75 static int read_buffer(unsigned char* data,int len){ | |
76 int len2=0; | |
77 int x; | |
78 while(len>0){ | |
79 if(full_buffers==0) break; // no more data buffered! | |
80 x=BUFFSIZE-buf_read_pos; | |
81 if(x>len) x=len; | |
82 memcpy(data+len2,buffer[buf_read]+buf_read_pos,x); | |
6184 | 83 SDL_MixAudio(data+len2, data+len2, x, volume); |
972 | 84 len2+=x; len-=x; |
85 buffered_bytes-=x; buf_read_pos+=x; | |
86 if(buf_read_pos>=BUFFSIZE){ | |
87 // block is empty, find next! | |
88 buf_read=(buf_read+1)%NUM_BUFS; | |
89 --full_buffers; | |
90 buf_read_pos=0; | |
91 } | |
92 } | |
93 return len2; | |
94 } | |
95 | |
96 // end ring buffer stuff | |
966 | 97 |
6956
0380dfad2db9
HPUX porting fixes - patch by Gansser, Martin <MGansser@rand.de>
arpi
parents:
6184
diff
changeset
|
98 #if defined(HPUX) || defined(sun) && defined(__svr4__) |
0380dfad2db9
HPUX porting fixes - patch by Gansser, Martin <MGansser@rand.de>
arpi
parents:
6184
diff
changeset
|
99 /* setenv is missing on solaris and HPUX */ |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
100 static void setenv(const char *name, const char *val, int _xx) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
101 { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
102 int len = strlen(name) + strlen(val) + 2; |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
103 char *env = malloc(len); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
104 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
105 if (env != NULL) { |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
106 strcpy(env, name); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
107 strcat(env, "="); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
108 strcat(env, val); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
109 putenv(env); |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
110 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
111 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
112 #endif |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
113 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
114 |
966 | 115 // to set/get/query special features/parameters |
116 static int control(int cmd,int arg){ | |
6184 | 117 switch (cmd) { |
118 case AOCONTROL_GET_VOLUME: | |
119 { | |
120 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
121 vol->left = vol->right = (float)((volume + 127)/2.55); | |
122 return CONTROL_OK; | |
123 } | |
124 case AOCONTROL_SET_VOLUME: | |
125 { | |
126 float diff; | |
127 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
128 diff = (vol->left+vol->right) / 2; | |
129 volume = (int)(diff * 2.55) - 127; | |
130 return CONTROL_OK; | |
131 } | |
132 } | |
133 return -1; | |
966 | 134 } |
135 | |
972 | 136 // SDL Callback function |
137 void outputaudio(void *unused, Uint8 *stream, int len) { | |
138 //SDL_MixAudio(stream, read_buffer(buffers, len), len, SDL_MIX_MAXVOLUME); | |
1066 | 139 //if(!full_buffers) printf("SDL: Buffer underrun!\n"); |
140 | |
972 | 141 read_buffer(stream, len); |
1066 | 142 //printf("SDL: Full Buffers: %i\n", full_buffers); |
966 | 143 } |
144 | |
145 // open & setup audio device | |
146 // return: 1=success 0=fail | |
147 static int init(int rate,int channels,int format,int flags){ | |
148 | |
972 | 149 /* SDL Audio Specifications */ |
150 SDL_AudioSpec aspec; | |
151 | |
152 int i; | |
153 /* Allocate ring-buffer memory */ | |
983 | 154 for(i=0;i<NUM_BUFS;i++) buffer[i]=(unsigned char *) malloc(BUFFSIZE); |
972 | 155 |
1070 | 156 printf("SDL: Samplerate: %iHz Channels: %s Format %s\n", rate, (channels > 1) ? "Stereo" : "Mono", audio_out_format_name(format)); |
983 | 157 |
1189 | 158 if(ao_subdevice) { |
159 setenv("SDL_AUDIODRIVER", ao_subdevice, 1); | |
160 printf("SDL: using %s audio driver\n", ao_subdevice); | |
161 } | |
3095 | 162 |
3137 | 163 ao_data.bps=channels*rate; |
3095 | 164 if(format != AFMT_U8 && format != AFMT_S8) |
165 ao_data.bps*=2; | |
966 | 166 |
1066 | 167 /* The desired audio format (see SDL_AudioSpec) */ |
168 switch(format) { | |
169 case AFMT_U8: | |
170 aspec.format = AUDIO_U8; | |
171 break; | |
172 case AFMT_S16_LE: | |
173 aspec.format = AUDIO_S16LSB; | |
174 break; | |
175 case AFMT_S16_BE: | |
176 aspec.format = AUDIO_S16MSB; | |
177 break; | |
178 case AFMT_S8: | |
179 aspec.format = AUDIO_S8; | |
180 break; | |
181 case AFMT_U16_LE: | |
182 aspec.format = AUDIO_U16LSB; | |
183 break; | |
184 case AFMT_U16_BE: | |
185 aspec.format = AUDIO_U16MSB; | |
186 break; | |
187 default: | |
188 printf("SDL: Unsupported audio format: 0x%x.\n", format); | |
189 return 0; | |
190 } | |
191 | |
966 | 192 /* The desired audio frequency in samples-per-second. */ |
193 aspec.freq = rate; | |
194 | |
195 /* Number of channels (mono/stereo) */ | |
196 aspec.channels = channels; | |
197 | |
198 /* The desired size of the audio buffer in samples. This number should be a power of two, and may be adjusted by the audio driver to a value more suitable for the hardware. Good values seem to range between 512 and 8192 inclusive, depending on the application and CPU speed. Smaller values yield faster response time, but can lead to underflow if the application is doing heavy processing and cannot fill the audio buffer in time. A stereo sample consists of both right and left channels in LR ordering. Note that the number of samples is directly related to time by the following formula: ms = (samples*1000)/freq */ | |
972 | 199 aspec.samples = SAMPLESIZE; |
966 | 200 |
201 /* This should be set to a function that will be called when the audio device is ready for more data. It is passed a pointer to the audio buffer, and the length in bytes of the audio buffer. This function usually runs in a separate thread, and so you should protect data structures that it accesses by calling SDL_LockAudio and SDL_UnlockAudio in your code. The callback prototype is: | |
202 void callback(void *userdata, Uint8 *stream, int len); userdata is the pointer stored in userdata field of the SDL_AudioSpec. stream is a pointer to the audio buffer you want to fill with information and len is the length of the audio buffer in bytes. */ | |
972 | 203 aspec.callback = outputaudio; |
966 | 204 |
205 /* This pointer is passed as the first parameter to the callback function. */ | |
206 aspec.userdata = NULL; | |
207 | |
972 | 208 /* initialize the SDL Audio system */ |
209 if (SDL_Init (SDL_INIT_AUDIO/*|SDL_INIT_NOPARACHUTE*/)) { | |
210 printf("SDL: Initializing of SDL Audio failed: %s.\n", SDL_GetError()); | |
211 return 0; | |
212 } | |
213 | |
966 | 214 /* Open the audio device and start playing sound! */ |
215 if(SDL_OpenAudio(&aspec, NULL) < 0) { | |
216 printf("SDL: Unable to open audio: %s\n", SDL_GetError()); | |
217 return(0); | |
218 } | |
219 | |
983 | 220 if(verbose) printf("SDL: buf size = %d\n",aspec.size); |
3095 | 221 if(ao_data.buffersize==-1) ao_data.buffersize=aspec.size; |
974 | 222 |
966 | 223 /* unsilence audio, if callback is ready */ |
224 SDL_PauseAudio(0); | |
225 | |
226 return 1; | |
227 } | |
228 | |
229 // close audio device | |
230 static void uninit(){ | |
983 | 231 if(verbose) printf("SDL: Audio Subsystem shutting down!\n"); |
966 | 232 SDL_CloseAudio(); |
972 | 233 SDL_QuitSubSystem(SDL_INIT_AUDIO); |
966 | 234 } |
235 | |
236 // stop playing and empty buffers (for seeking/pause) | |
237 static void reset(){ | |
1066 | 238 |
1091 | 239 //printf("SDL: reset called!\n"); |
1066 | 240 |
972 | 241 /* Reset ring-buffer state */ |
242 buf_read=0; | |
243 buf_write=0; | |
244 buf_read_pos=0; | |
245 buf_write_pos=0; | |
246 | |
247 full_buffers=0; | |
248 buffered_bytes=0; | |
966 | 249 |
250 } | |
251 | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
252 // stop playing, keep buffers (for pause) |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
253 static void audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
254 { |
1070 | 255 |
1091 | 256 //printf("SDL: audio_pause called!\n"); |
257 SDL_LockAudio(); | |
1070 | 258 |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
259 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
260 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
261 // resume playing, after audio_pause() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
262 static void audio_resume() |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
263 { |
1091 | 264 //printf("SDL: audio_resume called!\n"); |
265 SDL_UnlockAudio(); | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
266 } |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
267 |
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
993
diff
changeset
|
268 |
966 | 269 // return: how many bytes can be played without blocking |
270 static int get_space(){ | |
972 | 271 return (NUM_BUFS-full_buffers)*BUFFSIZE - buf_write_pos; |
966 | 272 } |
273 | |
274 // plays 'len' bytes of 'data' | |
275 // it should round it down to outburst*n | |
276 // return: number of bytes played | |
277 static int play(void* data,int len,int flags){ | |
972 | 278 |
985 | 279 #if 0 |
972 | 280 int ret; |
966 | 281 |
972 | 282 /* Audio locking prohibits call of outputaudio */ |
966 | 283 SDL_LockAudio(); |
972 | 284 // copy audio stream into ring-buffer |
285 ret = write_buffer(data, len); | |
966 | 286 SDL_UnlockAudio(); |
287 | |
972 | 288 return ret; |
289 #else | |
290 return write_buffer(data, len); | |
291 #endif | |
966 | 292 } |
293 | |
3095 | 294 // return: delay in seconds between first and last sample in buffer |
295 static float get_delay(){ | |
296 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps; | |
966 | 297 } |
298 | |
299 | |
300 | |
301 | |
302 | |
303 |