Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / libao2 / ao_mpegpes.c
blob59cba696ec82c5ac51de5db176bac2e4b2108568
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <inttypes.h>
9 #include <errno.h>
11 #include "config.h"
13 #ifdef HAVE_DVB_HEAD
14 #define HAVE_DVB 1
15 #endif
17 #ifdef HAVE_DVB
18 #include <sys/poll.h>
19 #include <sys/ioctl.h>
20 #endif
22 #include "audio_out.h"
23 #include "audio_out_internal.h"
25 #include "libaf/af_format.h"
26 #include "libmpdemux/mpeg_packetizer.h"
27 #include "subopt-helper.h"
29 #include "mp_msg.h"
30 #include "help_mp.h"
32 #ifdef HAVE_DVB
33 #ifndef HAVE_DVB_HEAD
34 #include <ost/audio.h>
35 audioMixer_t dvb_mixer={255,255};
36 #else
37 #include <linux/dvb/audio.h>
38 audio_mixer_t dvb_mixer={255,255};
39 #endif
40 #endif
42 #define true 1
43 #define false 0
45 extern int vo_mpegpes_fd;
46 int vo_mpegpes_fd2 = -1;
48 #include <errno.h>
50 static ao_info_t info =
52 #ifdef HAVE_DVB
53 "DVB audio output",
54 #else
55 "Mpeg-PES audio output",
56 #endif
57 "mpegpes",
58 "A'rpi",
62 LIBAO_EXTERN(mpegpes)
65 // to set/get/query special features/parameters
66 static int control(int cmd,void *arg){
67 #ifdef HAVE_DVB
68 switch(cmd){
69 case AOCONTROL_GET_VOLUME:
70 if(vo_mpegpes_fd2>=0){
71 ((ao_control_vol_t*)(arg))->left=dvb_mixer.volume_left/2.56;
72 ((ao_control_vol_t*)(arg))->right=dvb_mixer.volume_right/2.56;
73 return CONTROL_OK;
75 return CONTROL_ERROR;
76 case AOCONTROL_SET_VOLUME:
77 if(vo_mpegpes_fd2>=0){
78 dvb_mixer.volume_left=((ao_control_vol_t*)(arg))->left*2.56;
79 dvb_mixer.volume_right=((ao_control_vol_t*)(arg))->right*2.56;
80 if(dvb_mixer.volume_left>255) dvb_mixer.volume_left=255;
81 if(dvb_mixer.volume_right>255) dvb_mixer.volume_right=255;
82 // printf("Setting DVB volume: %d ; %d \n",dvb_mixer.volume_left,dvb_mixer.volume_right);
83 if ( (ioctl(vo_mpegpes_fd2,AUDIO_SET_MIXER, &dvb_mixer) < 0)){
84 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_MPEGPES_CantSetMixer,
85 strerror(errno));
86 return CONTROL_ERROR;
88 return CONTROL_OK;
90 return CONTROL_ERROR;
92 #endif
93 return CONTROL_UNKNOWN;
96 static int freq=0;
97 static int freq_id=0;
99 #ifdef HAVE_DVB
100 static int init_device(int card)
102 char ao_file[30];
103 #ifndef HAVE_DVB_HEAD
104 mp_msg(MSGT_VO,MSGL_INFO, "Opening /dev/ost/audio\n");
105 sprintf(ao_file, "/dev/ost/audio");
106 #else
107 mp_msg(MSGT_VO,MSGL_INFO, "Opening /dev/dvb/adapter%d/audio0\n", card);
108 sprintf(ao_file, "/dev/dvb/adapter%d/audio0", card);
109 #endif
110 if((vo_mpegpes_fd2 = open(ao_file,O_RDWR|O_NONBLOCK)) < 0)
112 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO DEVICE: %s\n", strerror(errno));
113 return -1;
115 if( (ioctl(vo_mpegpes_fd2,AUDIO_SELECT_SOURCE, AUDIO_SOURCE_MEMORY) < 0))
117 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SELECT SOURCE: %s\n", strerror(errno));
118 return -1;
120 if((ioctl(vo_mpegpes_fd2,AUDIO_PLAY) < 0))
122 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO PLAY: %s\n", strerror(errno));
123 return -1;
125 if((ioctl(vo_mpegpes_fd2,AUDIO_SET_AV_SYNC, true) < 0))
127 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET AV SYNC: %s\n", strerror(errno));
128 return -1;
130 //FIXME: in vo_mpegpes audio was inited as MUTEd
131 if((ioctl(vo_mpegpes_fd2,AUDIO_SET_MUTE, false) < 0))
133 mp_msg(MSGT_VO, MSGL_ERR, "DVB AUDIO SET MUTE: %s\n", strerror(errno));
134 return -1;
136 return vo_mpegpes_fd2;
138 #endif
140 static int preinit(const char *arg)
142 int card = 1;
143 char *ao_file = NULL;
145 opt_t subopts[] = {
146 {"card", OPT_ARG_INT, &card, NULL},
147 {"file", OPT_ARG_MSTRZ, &ao_file, NULL},
148 {NULL}
151 if(subopt_parse(ao_subdevice, subopts) != 0)
153 mp_msg(MSGT_VO, MSGL_ERR, "AO_MPEGPES, Unrecognized options\n");
154 return -1;
156 if((card < 1) || (card > 4))
158 mp_msg(MSGT_VO, MSGL_ERR, "DVB card number must be between 1 and 4\n");
159 return -1;
161 card--;
163 #ifdef HAVE_DVB
164 if(!ao_file)
165 return init_device(card);
166 #else
167 if(!ao_file)
168 return vo_mpegpes_fd; //video fd
169 #endif
171 vo_mpegpes_fd2=open(ao_file,O_WRONLY|O_CREAT,0666);
172 if(vo_mpegpes_fd2<0)
174 mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes: %s\n", strerror(errno));
175 return -1;
177 return vo_mpegpes_fd2;
180 static int my_ao_write(unsigned char* data,int len){
181 int orig_len = len;
182 #ifdef HAVE_DVB
183 #define NFD 1
184 struct pollfd pfd[NFD];
186 pfd[0].fd = vo_mpegpes_fd2;
187 pfd[0].events = POLLOUT;
189 while(len>0){
190 if(poll(pfd,NFD,1)){
191 if(pfd[0].revents & POLLOUT){
192 int ret=write(vo_mpegpes_fd2,data,len);
193 if(ret<=0){
194 mp_msg(MSGT_VO, MSGL_ERR, "ao_mpegpes write: %s\n", strerror(errno));
195 usleep(0);
196 } else {
197 len-=ret;
198 data+=ret;
200 } else usleep(1000);
204 #else
205 if(vo_mpegpes_fd2<0) return 0; // no file
206 write(vo_mpegpes_fd2,data,len); // write to file
207 #endif
208 return orig_len;
212 // open & setup audio device
213 // return: 1=success 0=fail
214 static int init(int rate,int channels,int format,int flags){
215 if(preinit(NULL)<0) return 0;
217 ao_data.channels=2;
218 ao_data.outburst=2000;
219 switch(format){
220 case AF_FORMAT_S16_BE:
221 case AF_FORMAT_MPEG2:
222 case AF_FORMAT_AC3:
223 ao_data.format=format;
224 break;
225 default:
226 ao_data.format=AF_FORMAT_S16_BE;
229 switch(rate){
230 case 48000: freq_id=0;break;
231 case 96000: freq_id=1;break;
232 case 44100: freq_id=2;break;
233 case 32000: freq_id=3;break;
234 default:
235 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_MPEGPES_UnsupSamplerate, rate);
236 #if 0
237 if(rate>48000) rate=96000; else
238 if(rate>44100) rate=48000; else
239 if(rate>32000) rate=44100; else
240 rate=32000;
241 goto retry;
242 #else
243 rate=48000; freq_id=0;
244 #endif
247 ao_data.bps=rate*2*2;
248 freq=ao_data.samplerate=rate;
250 return 1;
253 // close audio device
254 static void uninit(int immed){
258 // stop playing and empty buffers (for seeking/pause)
259 static void reset(void){
263 // stop playing, keep buffers (for pause)
264 static void audio_pause(void)
266 // for now, just call reset();
267 reset();
270 // resume playing, after audio_pause()
271 static void audio_resume(void)
275 void send_pes_packet(unsigned char* data,int len,int id,int timestamp);
276 void send_lpcm_packet(unsigned char* data,int len,int id,int timestamp,int freq_id);
277 extern int vo_pts;
279 // return: how many bytes can be played without blocking
280 static int get_space(void){
281 float x=(float)(vo_pts-ao_data.pts)/90000.0;
282 int y;
283 //FIXME: is it correct?
284 if(vo_mpegpes_fd < 0) return 32000; //not using -vo mpegpes
285 // printf("vo_pts: %5.3f ao_pts: %5.3f\n",vo_pts/90000.0,ao_data.pts/90000.0);
286 if(x<=0) return 0;
287 y=freq*4*x;y/=ao_data.outburst;y*=ao_data.outburst;
288 if(y>32000) y=32000;
289 // printf("diff: %5.3f -> %d \n",x,y);
290 return y;
293 // plays 'len' bytes of 'data'
294 // it should round it down to outburst*n
295 // return: number of bytes played
296 static int play(void* data,int len,int flags){
297 // printf("\nao_mpegpes: play(%d) freq=%d\n",len,freq_id);
298 if(ao_data.format==AF_FORMAT_MPEG2)
299 send_mpeg_pes_packet (data, len, 0x1C0, ao_data.pts, 1, my_ao_write);
300 else {
301 int i;
302 unsigned short *s=data;
303 // if(len>2000) len=2000;
304 // printf("ao_mpegpes: len=%d \n",len);
305 if(ao_data.format==AF_FORMAT_AC3)
306 for(i=0;i<len/2;i++) s[i]=(s[i]>>8)|(s[i]<<8); // le<->be
307 send_mpeg_lpcm_packet(data, len, 0xA0, ao_data.pts, freq_id, my_ao_write);
309 return len;
312 // return: delay in seconds between first and last sample in buffer
313 static float get_delay(void){
315 return 0.0;