2 * PCM audio output driver
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "libavutil/common.h"
29 #include "subopt-helper.h"
30 #include "libaf/af_format.h"
31 #include "libaf/reorder_ch.h"
32 #include "audio_out.h"
33 #include "audio_out_internal.h"
38 // for GetFileType to detect pipes
42 static const ao_info_t info
=
44 "RAW PCM/WAVE file writer audio output",
54 static char *ao_outputfilename
= NULL
;
55 static int ao_pcm_waveheader
= 1;
58 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
59 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
60 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
61 #define WAV_ID_DATA 0x61746164 /* "data" */
62 #define WAV_ID_PCM 0x0001
63 #define WAV_ID_FLOAT_PCM 0x0003
75 uint32_t bytes_per_second
;
82 /* init with default values */
83 static struct WaveHeader wavhdr
;
84 static uint64_t data_length
;
86 static FILE *fp
= NULL
;
88 // to set/get/query special features/parameters
89 static int control(int cmd
,void *arg
){
93 // open & setup audio device
94 // return: 1=success 0=fail
95 static int init(int rate
,int channels
,int format
,int flags
){
97 const opt_t subopts
[] = {
98 {"waveheader", OPT_ARG_BOOL
, &ao_pcm_waveheader
, NULL
},
99 {"file", OPT_ARG_MSTRZ
, &ao_outputfilename
, NULL
},
100 {"fast", OPT_ARG_BOOL
, &fast
, NULL
},
104 ao_pcm_waveheader
= 1;
106 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
109 if (!ao_outputfilename
){
111 strdup(ao_pcm_waveheader
?"audiodump.wav":"audiodump.pcm");
114 if (ao_pcm_waveheader
)
116 // WAV files must have one of the following formats
120 case AF_FORMAT_S16_LE
:
121 case AF_FORMAT_S24_LE
:
122 case AF_FORMAT_S32_LE
:
123 case AF_FORMAT_FLOAT_LE
:
124 case AF_FORMAT_AC3_BE
:
125 case AF_FORMAT_AC3_LE
:
128 format
= AF_FORMAT_S16_LE
;
133 bits
= af_fmt2bits(format
);
135 ao_data
.outburst
= 65536;
136 ao_data
.buffersize
= 2*65536;
137 ao_data
.channels
=channels
;
138 ao_data
.samplerate
=rate
;
139 ao_data
.format
=format
;
140 ao_data
.bps
=channels
*rate
*(bits
/8);
142 wavhdr
.riff
= le2me_32(WAV_ID_RIFF
);
143 wavhdr
.wave
= le2me_32(WAV_ID_WAVE
);
144 wavhdr
.fmt
= le2me_32(WAV_ID_FMT
);
145 wavhdr
.fmt_length
= le2me_32(16);
146 wavhdr
.fmt_tag
= le2me_16(format
== AF_FORMAT_FLOAT_LE
? WAV_ID_FLOAT_PCM
: WAV_ID_PCM
);
147 wavhdr
.channels
= le2me_16(ao_data
.channels
);
148 wavhdr
.sample_rate
= le2me_32(ao_data
.samplerate
);
149 wavhdr
.bytes_per_second
= le2me_32(ao_data
.bps
);
150 wavhdr
.bits
= le2me_16(bits
);
151 wavhdr
.block_align
= le2me_16(ao_data
.channels
* (bits
/ 8));
153 wavhdr
.data
= le2me_32(WAV_ID_DATA
);
154 wavhdr
.data_length
=le2me_32(0x7ffff000);
155 wavhdr
.file_length
= wavhdr
.data_length
+ sizeof(wavhdr
) - 8;
157 mp_tmsg(MSGT_AO
, MSGL_INFO
, "[AO PCM] File: %s (%s)\nPCM: Samplerate: %iHz Channels: %s Format %s\n", ao_outputfilename
,
158 (ao_pcm_waveheader
?"WAVE":"RAW PCM"), rate
,
159 (channels
> 1) ? "Stereo" : "Mono", af_fmt2str_short(format
));
160 mp_tmsg(MSGT_AO
, MSGL_INFO
, "[AO PCM] Info: Faster dumping is achieved with -vc null -vo null -ao pcm:fast\n[AO PCM] Info: To write WAVE files use -ao pcm:waveheader (default).\n");
162 fp
= fopen(ao_outputfilename
, "wb");
164 if(ao_pcm_waveheader
){ /* Reserve space for wave header */
165 fwrite(&wavhdr
,sizeof(wavhdr
),1,fp
);
169 mp_tmsg(MSGT_AO
, MSGL_ERR
, "[AO PCM] Failed to open %s for writing!\n",
174 // close audio device
175 static void uninit(int immed
){
177 if(ao_pcm_waveheader
){ /* Rewrite wave header */
180 // Windows, in its usual idiocy "emulates" seeks on pipes so it always looks
181 // like they work. So we have to detect them brute-force.
182 broken_seek
= GetFileType((HANDLE
)_get_osfhandle(_fileno(fp
))) != FILE_TYPE_DISK
;
184 if (broken_seek
|| fseek(fp
, 0, SEEK_SET
) != 0)
185 mp_msg(MSGT_AO
, MSGL_ERR
, "Could not seek to start, WAV size headers not updated!\n");
186 else if (data_length
> 0x7ffff000)
187 mp_msg(MSGT_AO
, MSGL_ERR
, "File larger than allowed for WAV files, may play truncated!\n");
189 wavhdr
.file_length
= data_length
+ sizeof(wavhdr
) - 8;
190 wavhdr
.file_length
= le2me_32(wavhdr
.file_length
);
191 wavhdr
.data_length
= le2me_32(data_length
);
192 fwrite(&wavhdr
,sizeof(wavhdr
),1,fp
);
196 if (ao_outputfilename
)
197 free(ao_outputfilename
);
198 ao_outputfilename
= NULL
;
201 // stop playing and empty buffers (for seeking/pause)
202 static void reset(void){
206 // stop playing, keep buffers (for pause)
207 static void audio_pause(void)
209 // for now, just call reset();
213 // resume playing, after audio_pause()
214 static void audio_resume(void)
218 // return: how many bytes can be played without blocking
219 static int get_space(void){
222 return ao_data
.pts
< vo_pts
+ fast
* 30000 ? ao_data
.outburst
: 0;
223 return ao_data
.outburst
;
226 // plays 'len' bytes of 'data'
227 // it should round it down to outburst*n
228 // return: number of bytes played
229 static int play(void* data
,int len
,int flags
){
231 // let libaf to do the conversion...
234 if (ao_data
.format
== AFMT_S16_LE
) {
235 unsigned short *buffer
= (unsigned short *) data
;
237 for(i
= 0; i
< len
/2; ++i
) {
238 buffer
[i
] = le2me_16(buffer
[i
]);
243 if (ao_data
.channels
== 5 || ao_data
.channels
== 6 || ao_data
.channels
== 8) {
244 int frame_size
= le2me_16(wavhdr
.bits
) / 8;
245 len
-= len
% (frame_size
* ao_data
.channels
);
246 reorder_channel_nch(data
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
247 AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT
,
249 len
/ frame_size
, frame_size
);
252 //printf("PCM: Writing chunk!\n");
253 fwrite(data
,len
,1,fp
);
255 if(ao_pcm_waveheader
)
261 // return: delay in seconds between first and last sample in buffer
262 static float get_delay(void){