Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libao2 / ao_pcm.c
blobbd66ed0dab3823ddb14fbe05a51f325ac0427755
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"
38 static ao_info_t info =
40 "RAW PCM/WAVE file writer audio output",
41 "pcm",
42 "Atmosfear",
46 LIBAO_EXTERN(pcm)
48 extern int vo_pts;
50 static char *ao_outputfilename = NULL;
51 static int ao_pcm_waveheader = 1;
52 static int fast = 0;
54 #define WAV_ID_RIFF 0x46464952 /* "RIFF" */
55 #define WAV_ID_WAVE 0x45564157 /* "WAVE" */
56 #define WAV_ID_FMT 0x20746d66 /* "fmt " */
57 #define WAV_ID_DATA 0x61746164 /* "data" */
58 #define WAV_ID_PCM 0x0001
59 #define WAV_ID_FLOAT_PCM 0x0003
61 struct WaveHeader
63 uint32_t riff;
64 uint32_t file_length;
65 uint32_t wave;
66 uint32_t fmt;
67 uint32_t fmt_length;
68 uint16_t fmt_tag;
69 uint16_t channels;
70 uint32_t sample_rate;
71 uint32_t bytes_per_second;
72 uint16_t block_align;
73 uint16_t bits;
74 uint32_t data;
75 uint32_t data_length;
78 /* init with default values */
79 static struct WaveHeader wavhdr;
81 static FILE *fp = NULL;
83 // to set/get/query special features/parameters
84 static int control(int cmd,void *arg){
85 return -1;
88 // open & setup audio device
89 // return: 1=success 0=fail
90 static int init(int rate,int channels,int format,int flags){
91 int bits;
92 opt_t subopts[] = {
93 {"waveheader", OPT_ARG_BOOL, &ao_pcm_waveheader, NULL},
94 {"file", OPT_ARG_MSTRZ, &ao_outputfilename, NULL},
95 {"fast", OPT_ARG_BOOL, &fast, NULL},
96 {NULL}
98 // set defaults
99 ao_pcm_waveheader = 1;
101 if (subopt_parse(ao_subdevice, subopts) != 0) {
102 return 0;
104 if (!ao_outputfilename){
105 ao_outputfilename =
106 strdup(ao_pcm_waveheader?"audiodump.wav":"audiodump.pcm");
109 bits=8;
110 switch(format){
111 case AF_FORMAT_S32_BE:
112 format=AF_FORMAT_S32_LE;
113 case AF_FORMAT_S32_LE:
114 bits=32;
115 break;
116 case AF_FORMAT_FLOAT_BE:
117 format=AF_FORMAT_FLOAT_LE;
118 case AF_FORMAT_FLOAT_LE:
119 bits=32;
120 break;
121 case AF_FORMAT_S8:
122 format=AF_FORMAT_U8;
123 case AF_FORMAT_U8:
124 break;
125 case AF_FORMAT_AC3:
126 bits=16;
127 break;
128 default:
129 format=AF_FORMAT_S16_LE;
130 bits=16;
131 break;
134 ao_data.outburst = 65536;
135 ao_data.buffersize= 2*65536;
136 ao_data.channels=channels;
137 ao_data.samplerate=rate;
138 ao_data.format=format;
139 ao_data.bps=channels*rate*(bits/8);
141 wavhdr.riff = le2me_32(WAV_ID_RIFF);
142 wavhdr.wave = le2me_32(WAV_ID_WAVE);
143 wavhdr.fmt = le2me_32(WAV_ID_FMT);
144 wavhdr.fmt_length = le2me_32(16);
145 wavhdr.fmt_tag = le2me_16(format == AF_FORMAT_FLOAT_LE ? WAV_ID_FLOAT_PCM : WAV_ID_PCM);
146 wavhdr.channels = le2me_16(ao_data.channels);
147 wavhdr.sample_rate = le2me_32(ao_data.samplerate);
148 wavhdr.bytes_per_second = le2me_32(ao_data.bps);
149 wavhdr.bits = le2me_16(bits);
150 wavhdr.block_align = le2me_16(ao_data.channels * (bits / 8));
152 wavhdr.data = le2me_32(WAV_ID_DATA);
153 wavhdr.data_length=le2me_32(0x7ffff000);
154 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
156 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_FileInfo, ao_outputfilename,
157 (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate,
158 (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
159 mp_msg(MSGT_AO, MSGL_INFO, MSGTR_AO_PCM_HintInfo);
161 fp = fopen(ao_outputfilename, "wb");
162 if(fp) {
163 if(ao_pcm_waveheader){ /* Reserve space for wave header */
164 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
165 wavhdr.file_length=wavhdr.data_length=0;
167 return 1;
169 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_PCM_CantOpenOutputFile,
170 ao_outputfilename);
171 return 0;
174 // close audio device
175 static void uninit(int immed){
177 if(ao_pcm_waveheader && fseek(fp, 0, SEEK_SET) == 0){ /* Write wave header */
178 wavhdr.file_length = wavhdr.data_length + sizeof(wavhdr) - 8;
179 wavhdr.file_length = le2me_32(wavhdr.file_length);
180 wavhdr.data_length = le2me_32(wavhdr.data_length);
181 fwrite(&wavhdr,sizeof(wavhdr),1,fp);
183 fclose(fp);
184 if (ao_outputfilename)
185 free(ao_outputfilename);
186 ao_outputfilename = NULL;
189 // stop playing and empty buffers (for seeking/pause)
190 static void reset(void){
194 // stop playing, keep buffers (for pause)
195 static void audio_pause(void)
197 // for now, just call reset();
198 reset();
201 // resume playing, after audio_pause()
202 static void audio_resume(void)
206 // return: how many bytes can be played without blocking
207 static int get_space(void){
209 if(vo_pts)
210 return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0;
211 return ao_data.outburst;
214 // plays 'len' bytes of 'data'
215 // it should round it down to outburst*n
216 // return: number of bytes played
217 static int play(void* data,int len,int flags){
219 // let libaf to do the conversion...
220 #if 0
221 //#ifdef WORDS_BIGENDIAN
222 if (ao_data.format == AFMT_S16_LE) {
223 unsigned short *buffer = (unsigned short *) data;
224 register int i;
225 for(i = 0; i < len/2; ++i) {
226 buffer[i] = le2me_16(buffer[i]);
229 #endif
231 if (ao_data.channels == 6 || ao_data.channels == 5) {
232 int frame_size = le2me_16(wavhdr.bits) / 8;
233 len -= len % (frame_size * ao_data.channels);
234 reorder_channel_nch(data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
235 AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
236 ao_data.channels,
237 len / frame_size, frame_size);
240 //printf("PCM: Writing chunk!\n");
241 fwrite(data,len,1,fp);
243 if(ao_pcm_waveheader)
244 wavhdr.data_length += len;
246 return len;
249 // return: delay in seconds between first and last sample in buffer
250 static float get_delay(void){
252 return 0.0;