Use correct terminal "clear line" code
[mplayer.git] / libao2 / ao_dxr2.c
blobdc0e762362cf3997eac48189eb3dfd63f081ef45
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/ioctl.h>
5 #include <inttypes.h>
6 #include <dxr2ioctl.h>
7 #include "config.h"
8 #include "mp_msg.h"
9 #include "help_mp.h"
10 #include "libavutil/common.h"
11 #include "mpbswap.h"
13 #include "audio_out.h"
14 #include "audio_out_internal.h"
15 #include "libaf/af_format.h"
16 #include "libmpdemux/mpeg_packetizer.h"
19 static ao_info_t info =
21 "DXR2 audio output",
22 "dxr2",
23 "Tobias Diedrich <ranma+mplayer@tdiedrich.de>",
27 LIBAO_EXTERN(dxr2)
29 static int volume=19;
30 static int last_freq_id = -1;
31 extern int dxr2_fd;
33 // to set/get/query special features/parameters
34 static int control(int cmd,void *arg){
35 switch(cmd){
36 case AOCONTROL_GET_VOLUME:
37 if(dxr2_fd > 0) {
38 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
39 vol->left = vol->right = volume * 19.0 / 100.0;
40 return CONTROL_OK;
42 return CONTROL_ERROR;
43 case AOCONTROL_SET_VOLUME:
44 if(dxr2_fd > 0) {
45 dxr2_oneArg_t v;
46 float diff;
47 ao_control_vol_t* vol = (ao_control_vol_t*)arg;
48 // We need this trick because the volume stepping is often too small
49 diff = ((vol->left+vol->right) / 2 - (volume*19.0/100.0)) * 19.0 / 100.0;
50 v.arg = volume + (diff > 0 ? ceil(diff) : floor(diff));
51 if(v.arg > 19) v.arg = 19;
52 if(v.arg < 0) v.arg = 0;
53 if(v.arg != volume) {
54 volume = v.arg;
55 if( ioctl(dxr2_fd,DXR2_IOC_SET_AUDIO_VOLUME,&v) < 0) {
56 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_DXR2_SetVolFailed,volume);
57 return CONTROL_ERROR;
60 return CONTROL_OK;
62 return CONTROL_ERROR;
64 return CONTROL_UNKNOWN;
67 static int freq=0;
68 static int freq_id=0;
70 // open & setup audio device
71 // return: 1=success 0=fail
72 static int init(int rate,int channels,int format,int flags){
74 if(dxr2_fd <= 0)
75 return 0;
77 last_freq_id = -1;
79 ao_data.outburst=2048;
80 ao_data.samplerate=rate;
81 ao_data.channels=channels;
82 ao_data.buffersize=2048;
83 ao_data.bps=rate*4;
84 ao_data.format=format;
85 freq=rate;
87 switch(rate){
88 case 48000:
89 freq_id=DXR2_AUDIO_FREQ_48;
90 break;
91 case 96000:
92 freq_id=DXR2_AUDIO_FREQ_96;
93 break;
94 case 44100:
95 freq_id=DXR2_AUDIO_FREQ_441;
96 break;
97 case 32000:
98 freq_id=DXR2_AUDIO_FREQ_32;
99 break;
100 case 22050:
101 freq_id=DXR2_AUDIO_FREQ_2205;
102 break;
103 #ifdef DXR2_AUDIO_FREQ_24
104 // This is not yet in the dxr2 driver CVS
105 // you can get the patch at
106 // http://www.tdiedrich.de/~ranma/patches/dxr2.pcm1723.20020513
107 case 24000:
108 freq_id=DXR2_AUDIO_FREQ_24;
109 break;
110 case 64000:
111 freq_id=DXR2_AUDIO_FREQ_64;
112 break;
113 case 88200:
114 freq_id=DXR2_AUDIO_FREQ_882;
115 break;
116 #endif
117 default:
118 mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_DXR2_UnsupSamplerate,rate);
119 return 0;
122 return 1;
125 // close audio device
126 static void uninit(int immed){
130 // stop playing and empty buffers (for seeking/pause)
131 static void reset(void){
135 // stop playing, keep buffers (for pause)
136 static void audio_pause(void)
138 // for now, just call reset();
139 reset();
142 // resume playing, after audio_pause()
143 static void audio_resume(void)
147 extern int vo_pts;
148 // return: how many bytes can be played without blocking
149 static int get_space(void){
150 float x=(float)(vo_pts-ao_data.pts)/90000.0;
151 int y;
152 if(x<=0) return 0;
153 y=freq*4*x;y/=ao_data.outburst;y*=ao_data.outburst;
154 if(y>32768) y=32768;
155 return y;
158 static void dxr2_send_lpcm_packet(unsigned char* data,int len,int id,unsigned int timestamp,int freq_id)
160 extern int write_dxr2(const unsigned char *data, int len);
162 if(dxr2_fd < 0) {
163 mp_msg(MSGT_AO,MSGL_ERR,"DXR2 fd is not valid\n");
164 return;
167 if(last_freq_id != freq_id) {
168 ioctl(dxr2_fd, DXR2_IOC_SET_AUDIO_SAMPLE_FREQUENCY, &freq_id);
169 last_freq_id = freq_id;
172 send_mpeg_lpcm_packet (data, len, id, timestamp, freq_id, write_dxr2);
175 // plays 'len' bytes of 'data'
176 // it should round it down to outburst*n
177 // return: number of bytes played
178 static int play(void* data,int len,int flags){
179 extern int write_dxr2(const unsigned char *data, int len);
181 // MPEG and AC3 don't work :-(
182 if(ao_data.format==AF_FORMAT_MPEG2)
183 send_mpeg_ps_packet (data, len, 0xC0, ao_data.pts, 2, write_dxr2);
184 else if(ao_data.format==AF_FORMAT_AC3)
185 send_mpeg_ps_packet (data, len, 0x80, ao_data.pts, 2, write_dxr2);
186 else {
187 int i;
188 //unsigned short *s=data;
189 uint16_t *s=data;
190 #ifndef WORDS_BIGENDIAN
191 for(i=0;i<len/2;i++) s[i] = bswap_16(s[i]);
192 #endif
193 dxr2_send_lpcm_packet(data,len,0xA0,ao_data.pts-10000,freq_id);
195 return len;
198 // return: delay in seconds between first and last sample in buffer
199 static float get_delay(void){
201 return 0.0;