Partial (low bits ignored, no direct transcoding into other RGB formats) support
[mplayer/glamo.git] / libao2 / ao_pcm.c
bloba49a963a302709a35f8f9ed8406875e5d8a1aa8e
1 /*
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.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "libavutil/common.h"
28 #include "mpbswap.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"
34 #include "mp_msg.h"
35 #include "help_mp.h"
37 #ifdef __MINGW32__
38 // for GetFileType to detect pipes
39 #include <windows.h>
40 #endif
42 static const ao_info_t info =
44 "RAW PCM/WAVE file writer audio output",
45 "pcm",
46 "Atmosfear",
50 LIBAO_EXTERN(pcm)
52 extern int vo_pts;
54 static char *ao_outputfilename = NULL;
55 static int ao_pcm_waveheader = 1;
56 static int fast = 0;
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
65 struct WaveHeader
67 uint32_t riff;
68 uint32_t file_length;
69 uint32_t wave;
70 uint32_t fmt;
71 uint32_t fmt_length;
72 uint16_t fmt_tag;
73 uint16_t channels;
74 uint32_t sample_rate;
75 uint32_t bytes_per_second;
76 uint16_t block_align;
77 uint16_t bits;
78 uint32_t data;
79 uint32_t data_length;
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){
90 return -1;
93 // open & setup audio device
94 // return: 1=success 0=fail
95 static int init(int rate,int channels,int format,int flags){
96 int bits;
97 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},
101 {NULL}
103 // set defaults
104 ao_pcm_waveheader = 1;
106 if (subopt_parse(ao_subdevice, subopts) != 0) {
107 return 0;
109 if (!ao_outputfilename){
110 ao_outputfilename =
111 strdup(ao_pcm_waveheader?"audiodump.wav":"audiodump.pcm");
114 bits=8;
115 switch(format){
116 case AF_FORMAT_S32_BE:
117 format=AF_FORMAT_S32_LE;
118 case AF_FORMAT_S32_LE:
119 bits=32;
120 break;
121 case AF_FORMAT_FLOAT_BE:
122 format=AF_FORMAT_FLOAT_LE;
123 case AF_FORMAT_FLOAT_LE:
124 bits=32;
125 break;
126 case AF_FORMAT_S8:
127 format=AF_FORMAT_U8;
128 case AF_FORMAT_U8:
129 break;
130 case AF_FORMAT_AC3:
131 bits=16;
132 break;
133 default:
134 format=AF_FORMAT_S16_LE;
135 bits=16;
136 break;
139 ao_data.outburst = 65536;
140 ao_data.buffersize= 2*65536;
141 ao_data.channels=channels;
142 ao_data.samplerate=rate;
143 ao_data.format=format;
144 ao_data.bps=channels*rate*(bits/8);
146 wavhdr.riff = le2me_32(WAV_ID_RIFF);
147 wavhdr.wave = le2me_32(WAV_ID_WAVE);
148 wavhdr.fmt = le2me_32(WAV_ID_FMT);
149 wavhdr.fmt_length = le2me_32(16);
150 wavhdr.fmt_tag = le2me_16(format == AF_FORMAT_FLOAT_LE ? WAV_ID_FLOAT_PCM : WAV_ID_PCM);
151 wavhdr.channels = le2me_16(ao_data.channels);
152 wavhdr.sample_rate = le2me_32(ao_data.samplerate);
153 wavhdr.bytes_per_second = le2me_32(ao_data.bps);
154 wavhdr.bits = le2me_16(bits);
155 wavhdr.block_align = le2me_16(ao_data.channels * (bits / 8));
157 wavhdr.data = le2me_32(WAV_ID_DATA);
158 wavhdr.data_length=le2me_32(0x7ffff000);
159 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
161 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename,
162 (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate,
163 (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
164 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_HintInfo);
166 fp = fopen(ao_outputfilename, "wb");
167 if(fp) {
168 if(ao_pcm_waveheader){ /* Reserve space for wave header */
169 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
171 return 1;
173 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile,
174 ao_outputfilename);
175 return 0;
178 // close audio device
179 static void uninit(int immed){
181 if(ao_pcm_waveheader){ /* Rewrite wave header */
182 int broken_seek = 0;
183 #ifdef __MINGW32__
184 // Windows, in its usual idiocy "emulates" seeks on pipes so it always looks
185 // like they work. So we have to detect them brute-force.
186 broken_seek = GetFileType((HANDLE)_get_osfhandle(_fileno(fp))) != FILE_TYPE_DISK;
187 #endif
188 if (broken_seek || fseek(fp, 0, SEEK_SET) != 0)
189 mp_msg(MSGT_AO, MSGL_ERR, "Could not seek to start, WAV size headers not updated!\n");
190 else if (data_length > 0x7ffff000)
191 mp_msg(MSGT_AO, MSGL_ERR, "File larger than allowed for WAV files, may play truncated!\n");
192 else {
193 wavhdr.file_length = data_length + sizeof(wavhdr) - 8;
194 wavhdr.file_length = le2me_32(wavhdr.file_length);
195 wavhdr.data_length = le2me_32(data_length);
196 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
199 fclose(fp);
200 if (ao_outputfilename)
201 free(ao_outputfilename);
202 ao_outputfilename = NULL;
205 // stop playing and empty buffers (for seeking/pause)
206 static void reset(void){
210 // stop playing, keep buffers (for pause)
211 static void audio_pause(void)
213 // for now, just call reset();
214 reset();
217 // resume playing, after audio_pause()
218 static void audio_resume(void)
222 // return: how many bytes can be played without blocking
223 static int get_space(void){
225 if(vo_pts)
226 return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0;
227 return ao_data.outburst;
230 // plays 'len' bytes of 'data'
231 // it should round it down to outburst*n
232 // return: number of bytes played
233 static int play(void* data,int len,int flags){
235 // let libaf to do the conversion...
236 #if 0
237 //#ifdef WORDS_BIGENDIAN
238 if (ao_data.format == AFMT_S16_LE) {
239 unsigned short *buffer = (unsigned short *) data;
240 register int i;
241 for(i = 0; i < len/2; ++i) {
242 buffer[i] = le2me_16(buffer[i]);
245 #endif
247 if (ao_data.channels == 6 || ao_data.channels == 5) {
248 int frame_size = le2me_16(wavhdr.bits) / 8;
249 len -= len % (frame_size * ao_data.channels);
250 reorder_channel_nch(data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
251 AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
252 ao_data.channels,
253 len / frame_size, frame_size);
256 //printf("PCM: Writing chunk!\n");
257 fwrite(data,len,1,fp);
259 if(ao_pcm_waveheader)
260 data_length += len;
262 return len;
265 // return: delay in seconds between first and last sample in buffer
266 static float get_delay(void){
268 return 0.0;