Mercurial > mplayer.hg
annotate libao2/ao_pcm.c @ 23961:4e22889a6e35
Ugly hack to make xanlib.dll work.
See also http://samples.mplayerhq.hu/game-formats/wc4-xan/
author | reimar |
---|---|
date | Wed, 01 Aug 2007 03:30:38 +0000 |
parents | fa99b3d31d13 |
children | 7a6a7f47109f |
rev | line source |
---|---|
6237 | 1 #include "config.h" |
2 | |
1107 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
6237 | 5 #include <string.h> |
1107 | 6 |
21372 | 7 #include "libavutil/common.h" |
21507
fa99b3d31d13
Hack around libavutil/bswap.h compilation problems due to always_inline undefined.
reimar
parents:
21372
diff
changeset
|
8 #include "mpbswap.h" |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
9 #include "subopt-helper.h" |
14245 | 10 #include "libaf/af_format.h" |
1107 | 11 #include "audio_out.h" |
12 #include "audio_out_internal.h" | |
14123 | 13 #include "mp_msg.h" |
14 #include "help_mp.h" | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
15 |
1107 | 16 |
17 static ao_info_t info = | |
18 { | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
19 "RAW PCM/WAVE file writer audio output", |
1107 | 20 "pcm", |
21 "Atmosfear", | |
22 "" | |
23 }; | |
24 | |
25 LIBAO_EXTERN(pcm) | |
26 | |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
27 extern int vo_pts; |
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
28 |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
29 static char *ao_outputfilename = NULL; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
30 static int ao_pcm_waveheader = 1; |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
31 static int fast = 0; |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
32 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
33 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
34 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
35 #define WAV_ID_FMT 0x20746d66 /* "fmt " */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
36 #define WAV_ID_DATA 0x61746164 /* "data" */ |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
37 #define WAV_ID_PCM 0x0001 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
38 |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
39 struct WaveHeader |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
40 { |
11270 | 41 uint32_t riff; |
42 uint32_t file_length; | |
43 uint32_t wave; | |
44 uint32_t fmt; | |
45 uint32_t fmt_length; | |
46 uint16_t fmt_tag; | |
47 uint16_t channels; | |
48 uint32_t sample_rate; | |
49 uint32_t bytes_per_second; | |
50 uint16_t block_align; | |
51 uint16_t bits; | |
52 uint32_t data; | |
53 uint32_t data_length; | |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
54 }; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
55 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
56 /* init with default values */ |
21250
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
57 static struct WaveHeader wavhdr; |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
58 |
1107 | 59 static FILE *fp = NULL; |
60 | |
61 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9277
diff
changeset
|
62 static int control(int cmd,void *arg){ |
1107 | 63 return -1; |
64 } | |
65 | |
66 // open & setup audio device | |
67 // return: 1=success 0=fail | |
68 static int init(int rate,int channels,int format,int flags){ | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
69 int bits; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
70 opt_t subopts[] = { |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
71 {"waveheader", OPT_ARG_BOOL, &ao_pcm_waveheader, NULL}, |
14539 | 72 {"file", OPT_ARG_MSTRZ, &ao_outputfilename, NULL}, |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
73 {"fast", OPT_ARG_BOOL, &fast, NULL}, |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
74 {NULL} |
14300 | 75 }; |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
76 // set defaults |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
77 ao_pcm_waveheader = 1; |
18488
b23c0e341e3e
Move setting the output filename after the suboption parsing, otherwise it
diego
parents:
18092
diff
changeset
|
78 |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
79 if (subopt_parse(ao_subdevice, subopts) != 0) { |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
80 return 0; |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
81 } |
18500
9533f21d3f7e
Only set the default output filename when it was not passed on the command
diego
parents:
18488
diff
changeset
|
82 if (!ao_outputfilename){ |
9533f21d3f7e
Only set the default output filename when it was not passed on the command
diego
parents:
18488
diff
changeset
|
83 ao_outputfilename = |
9533f21d3f7e
Only set the default output filename when it was not passed on the command
diego
parents:
18488
diff
changeset
|
84 strdup(ao_pcm_waveheader?"audiodump.wav":"audiodump.pcm"); |
9533f21d3f7e
Only set the default output filename when it was not passed on the command
diego
parents:
18488
diff
changeset
|
85 } |
18488
b23c0e341e3e
Move setting the output filename after the suboption parsing, otherwise it
diego
parents:
18092
diff
changeset
|
86 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
87 /* bits is only equal to format if (format == 8) or (format == 16); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
88 this means that the following "if" is a kludge and should |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
89 really be a switch to be correct in all cases */ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
90 |
7658 | 91 bits=8; |
92 switch(format){ | |
14245 | 93 case AF_FORMAT_S8: |
94 format=AF_FORMAT_U8; | |
95 case AF_FORMAT_U8: | |
7658 | 96 break; |
97 default: | |
14245 | 98 format=AF_FORMAT_S16_LE; |
7658 | 99 bits=16; |
100 break; | |
101 } | |
102 | |
103 ao_data.outburst = 65536; | |
104 ao_data.buffersize= 2*65536; | |
105 ao_data.channels=channels; | |
106 ao_data.samplerate=rate; | |
107 ao_data.format=format; | |
108 ao_data.bps=channels*rate*(bits/8); | |
109 | |
21250
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
110 wavhdr.riff = le2me_32(WAV_ID_RIFF); |
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
111 wavhdr.wave = le2me_32(WAV_ID_WAVE); |
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
112 wavhdr.fmt = le2me_32(WAV_ID_FMT); |
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
113 wavhdr.fmt_length = le2me_32(16); |
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
114 wavhdr.fmt_tag = le2me_16(WAV_ID_PCM); |
7658 | 115 wavhdr.channels = le2me_16(ao_data.channels); |
116 wavhdr.sample_rate = le2me_32(ao_data.samplerate); | |
117 wavhdr.bytes_per_second = le2me_32(ao_data.bps); | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
118 wavhdr.bits = le2me_16(bits); |
16243
178b8b4a62c6
Set block_align in header, seems MatLab can not handle files without.
reimar
parents:
14539
diff
changeset
|
119 wavhdr.block_align = le2me_16(ao_data.channels * (bits / 8)); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
120 |
21250
7b1ab00ef2eb
le2me_32 is no longer a macro on PPC, and in general does not have to
reimar
parents:
18500
diff
changeset
|
121 wavhdr.data = le2me_32(WAV_ID_DATA); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
122 wavhdr.data_length=le2me_32(0x7ffff000); |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
123 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
124 |
14264 | 125 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename, |
126 (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate, | |
127 (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format)); | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
128 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_HintInfo); |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
129 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
130 fp = fopen(ao_outputfilename, "wb"); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
131 if(fp) { |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
132 if(ao_pcm_waveheader){ /* Reserve space for wave header */ |
6501 | 133 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
9277
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
134 wavhdr.file_length=wavhdr.data_length=0; |
d824f1a3c341
The "initialize wav header with infinite lenght" broke the
arpi
parents:
9223
diff
changeset
|
135 } |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
136 return 1; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
137 } |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
138 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile, |
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
139 ao_outputfilename); |
1107 | 140 return 0; |
141 } | |
142 | |
143 // close audio device | |
12145 | 144 static void uninit(int immed){ |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
145 |
6501 | 146 if(ao_pcm_waveheader && fseek(fp, 0, SEEK_SET) == 0){ /* Write wave header */ |
4914
de4074ab4e5f
good-looking fix by Tobias Diedrich <td@informatik.uni-hannover.de>
arpi
parents:
3095
diff
changeset
|
147 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
148 wavhdr.file_length = le2me_32(wavhdr.file_length); |
6762 | 149 wavhdr.data_length = le2me_32(wavhdr.data_length); |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
150 fwrite(&wavhdr,sizeof(wavhdr),1,fp); |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
151 } |
1107 | 152 fclose(fp); |
14298
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
153 if (ao_outputfilename) |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
154 free(ao_outputfilename); |
3c818342a02b
Add -ao pcm suboptions and remove -aofile and -waveheader options.
reimar
parents:
14264
diff
changeset
|
155 ao_outputfilename = NULL; |
1107 | 156 } |
157 | |
158 // stop playing and empty buffers (for seeking/pause) | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
159 static void reset(void){ |
1107 | 160 |
161 } | |
162 | |
163 // stop playing, keep buffers (for pause) | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
164 static void audio_pause(void) |
1107 | 165 { |
166 // for now, just call reset(); | |
167 reset(); | |
168 } | |
169 | |
170 // resume playing, after audio_pause() | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
171 static void audio_resume(void) |
1107 | 172 { |
173 } | |
174 | |
175 // return: how many bytes can be played without blocking | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
176 static int get_space(void){ |
1107 | 177 |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
178 if(vo_pts) |
18092
96fdfbad5b1a
-ao pcm:fast suboption for faster-than-realtime dumping
reimar
parents:
17566
diff
changeset
|
179 return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0; |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
180 return ao_data.outburst; |
1107 | 181 } |
182 | |
183 // plays 'len' bytes of 'data' | |
184 // it should round it down to outburst*n | |
185 // return: number of bytes played | |
186 static int play(void* data,int len,int flags){ | |
187 | |
7658 | 188 // let libaf to do the conversion... |
189 #if 0 | |
190 //#ifdef WORDS_BIGENDIAN | |
191 if (ao_data.format == AFMT_S16_LE) { | |
192 unsigned short *buffer = (unsigned short *) data; | |
193 register int i; | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
194 for(i = 0; i < len/2; ++i) { |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
195 buffer[i] = le2me_16(buffer[i]); |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
196 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
197 } |
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
198 #endif |
7658 | 199 |
1107 | 200 //printf("PCM: Writing chunk!\n"); |
201 fwrite(data,len,1,fp); | |
5837
7e082f42497a
- Fix for big apple architectures by Rogerio Brito, reworked by me to use bswap.h macros.
atmos4
parents:
4914
diff
changeset
|
202 |
1112
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
203 if(ao_pcm_waveheader) |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
204 wavhdr.data_length += len; |
b1cf1087ec33
Added support for writing wave files and specifying filename to write to.
atmosfear
parents:
1107
diff
changeset
|
205 |
1107 | 206 return len; |
207 } | |
208 | |
3095 | 209 // return: delay in seconds between first and last sample in buffer |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16243
diff
changeset
|
210 static float get_delay(void){ |
1107 | 211 |
3095 | 212 return 0.0; |
1107 | 213 } |
214 | |
215 | |
216 | |
217 | |
218 | |
219 |