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
64 #define WAV_ID_FORMAT_EXTENSIBLE 0xfffe
66 /* init with default values */
67 static uint64_t data_length
;
68 static FILE *fp
= NULL
;
71 static void fput16le(uint16_t val
, FILE *fp
) {
72 uint8_t bytes
[2] = {val
, val
>> 8};
73 fwrite(bytes
, 1, 2, fp
);
76 static void fput32le(uint32_t val
, FILE *fp
) {
77 uint8_t bytes
[4] = {val
, val
>> 8, val
>> 16, val
>> 24};
78 fwrite(bytes
, 1, 4, fp
);
81 static void write_wave_header(FILE *fp
, uint64_t data_length
) {
82 int use_waveex
= (ao_data
.channels
>= 5 && ao_data
.channels
<= 8);
83 uint16_t fmt
= (ao_data
.format
== AF_FORMAT_FLOAT_LE
) ? WAV_ID_FLOAT_PCM
: WAV_ID_PCM
;
84 uint32_t fmt_chunk_size
= use_waveex
? 40 : 16;
85 int bits
= af_fmt2bits(ao_data
.format
);
88 fput32le(WAV_ID_RIFF
, fp
);
89 // RIFF chunk size: 'WAVE' + 'fmt ' + 4 + fmt_chunk_size + data chunk hdr (8) + data length
90 fput32le(12 + fmt_chunk_size
+ 8 + data_length
, fp
);
91 fput32le(WAV_ID_WAVE
, fp
);
94 fput32le(WAV_ID_FMT
, fp
);
95 fput32le(fmt_chunk_size
, fp
);
96 fput16le(use_waveex
? WAV_ID_FORMAT_EXTENSIBLE
: fmt
, fp
);
97 fput16le(ao_data
.channels
, fp
);
98 fput32le(ao_data
.samplerate
, fp
);
99 fput32le(ao_data
.bps
, fp
);
100 fput16le(ao_data
.channels
* (bits
/ 8), fp
);
107 switch (ao_data
.channels
) {
109 fput32le(0x0607, fp
); // L R C Lb Rb
112 fput32le(0x060f, fp
); // L R C Lb Rb LFE
115 fput32le(0x0727, fp
); // L R C Cb Ls Rs LFE
118 fput32le(0x063f, fp
); // L R C Lb Rb Ls Rs LFE
121 // 2 bytes format + 14 bytes guid
123 fput32le(0x00100000, fp
);
124 fput32le(0xAA000080, fp
);
125 fput32le(0x719B3800, fp
);
129 fput32le(WAV_ID_DATA
, fp
);
130 fput32le(data_length
, fp
);
133 // to set/get/query special features/parameters
134 static int control(int cmd
,void *arg
){
138 // open & setup audio device
139 // return: 1=success 0=fail
140 static int init(int rate
,int channels
,int format
,int flags
){
141 const opt_t subopts
[] = {
142 {"waveheader", OPT_ARG_BOOL
, &ao_pcm_waveheader
, NULL
},
143 {"file", OPT_ARG_MSTRZ
, &ao_outputfilename
, NULL
},
144 {"fast", OPT_ARG_BOOL
, &fast
, NULL
},
148 ao_pcm_waveheader
= 1;
150 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
153 if (!ao_outputfilename
){
155 strdup(ao_pcm_waveheader
?"audiodump.wav":"audiodump.pcm");
158 if (ao_pcm_waveheader
)
160 // WAV files must have one of the following formats
164 case AF_FORMAT_S16_LE
:
165 case AF_FORMAT_S24_LE
:
166 case AF_FORMAT_S32_LE
:
167 case AF_FORMAT_FLOAT_LE
:
168 case AF_FORMAT_AC3_BE
:
169 case AF_FORMAT_AC3_LE
:
172 format
= AF_FORMAT_S16_LE
;
177 ao_data
.outburst
= 65536;
178 ao_data
.buffersize
= 2*65536;
179 ao_data
.channels
=channels
;
180 ao_data
.samplerate
=rate
;
181 ao_data
.format
=format
;
182 ao_data
.bps
=channels
*rate
*(af_fmt2bits(format
)/8);
184 mp_msg(MSGT_AO
, MSGL_INFO
, MSGTR_AO_PCM_FileInfo
, ao_outputfilename
,
185 (ao_pcm_waveheader
?"WAVE":"RAW PCM"), rate
,
186 (channels
> 1) ? "Stereo" : "Mono", af_fmt2str_short(format
));
187 mp_msg(MSGT_AO
, MSGL_INFO
, MSGTR_AO_PCM_HintInfo
);
189 fp
= fopen(ao_outputfilename
, "wb");
191 if(ao_pcm_waveheader
){ /* Reserve space for wave header */
192 write_wave_header(fp
, 0x7ffff000);
196 mp_msg(MSGT_AO
, MSGL_ERR
, MSGTR_AO_PCM_CantOpenOutputFile
,
201 // close audio device
202 static void uninit(int immed
){
204 if(ao_pcm_waveheader
){ /* Rewrite wave header */
207 // Windows, in its usual idiocy "emulates" seeks on pipes so it always looks
208 // like they work. So we have to detect them brute-force.
209 broken_seek
= GetFileType((HANDLE
)_get_osfhandle(_fileno(fp
))) != FILE_TYPE_DISK
;
211 if (broken_seek
|| fseek(fp
, 0, SEEK_SET
) != 0)
212 mp_msg(MSGT_AO
, MSGL_ERR
, "Could not seek to start, WAV size headers not updated!\n");
214 if (data_length
> 0xfffff000) {
215 mp_msg(MSGT_AO
, MSGL_ERR
, "File larger than allowed for WAV files, may play truncated!\n");
216 data_length
= 0xfffff000;
218 write_wave_header(fp
, data_length
);
222 if (ao_outputfilename
)
223 free(ao_outputfilename
);
224 ao_outputfilename
= NULL
;
227 // stop playing and empty buffers (for seeking/pause)
228 static void reset(void){
232 // stop playing, keep buffers (for pause)
233 static void audio_pause(void)
235 // for now, just call reset();
239 // resume playing, after audio_pause()
240 static void audio_resume(void)
244 // return: how many bytes can be played without blocking
245 static int get_space(void){
248 return ao_data
.pts
< vo_pts
+ fast
* 30000 ? ao_data
.outburst
: 0;
249 return ao_data
.outburst
;
252 // plays 'len' bytes of 'data'
253 // it should round it down to outburst*n
254 // return: number of bytes played
255 static int play(void* data
,int len
,int flags
){
257 // let libaf to do the conversion...
260 if (ao_data
.format
== AFMT_S16_LE
) {
261 unsigned short *buffer
= (unsigned short *) data
;
263 for(i
= 0; i
< len
/2; ++i
) {
264 buffer
[i
] = le2me_16(buffer
[i
]);
269 if (ao_data
.channels
== 5 || ao_data
.channels
== 6 || ao_data
.channels
== 8) {
270 int frame_size
= af_fmt2bits(ao_data
.format
) / 8;
271 len
-= len
% (frame_size
* ao_data
.channels
);
272 reorder_channel_nch(data
, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT
,
273 AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT
,
275 len
/ frame_size
, frame_size
);
278 //printf("PCM: Writing chunk!\n");
279 fwrite(data
,len
,1,fp
);
281 if(ao_pcm_waveheader
)
287 // return: delay in seconds between first and last sample in buffer
288 static float get_delay(void){