aboutsummaryrefslogtreecommitdiffhomepage
path: root/libao2/ao_pcm.c
blob: 64eda888f7074f58602ae3ae9f066e67dd2e0392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * PCM audio output driver
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libavutil/common.h"
#include "mpbswap.h"
#include "subopt-helper.h"
#include "libaf/af_format.h"
#include "libaf/reorder_ch.h"
#include "audio_out.h"
#include "audio_out_internal.h"
#include "mp_msg.h"

#ifdef __MINGW32__
// for GetFileType to detect pipes
#include <windows.h>
#endif

static const ao_info_t info =
{
    "RAW PCM/WAVE file writer audio output",
    "pcm",
    "Atmosfear",
    ""
};

LIBAO_EXTERN(pcm)

extern int vo_pts;

static char *ao_outputfilename = NULL;
static int ao_pcm_waveheader = 1;
static int fast = 0;

#define WAV_ID_RIFF 0x46464952 /* "RIFF" */
#define WAV_ID_WAVE 0x45564157 /* "WAVE" */
#define WAV_ID_FMT  0x20746d66 /* "fmt " */
#define WAV_ID_DATA 0x61746164 /* "data" */
#define WAV_ID_PCM  0x0001
#define WAV_ID_FLOAT_PCM  0x0003
#define WAV_ID_FORMAT_EXTENSIBLE 0xfffe

/* init with default values */
static uint64_t data_length;
static FILE *fp = NULL;


static void fput16le(uint16_t val, FILE *fp) {
    uint8_t bytes[2] = {val, val >> 8};
    fwrite(bytes, 1, 2, fp);
}

static void fput32le(uint32_t val, FILE *fp) {
    uint8_t bytes[4] = {val, val >> 8, val >> 16, val >> 24};
    fwrite(bytes, 1, 4, fp);
}

static void write_wave_header(FILE *fp, uint64_t data_length) {
    int use_waveex = (ao_data.channels >= 5 && ao_data.channels <= 8);
    uint16_t fmt = (ao_data.format == AF_FORMAT_FLOAT_LE) ? WAV_ID_FLOAT_PCM : WAV_ID_PCM;
    uint32_t fmt_chunk_size = use_waveex ? 40 : 16;
    int bits = af_fmt2bits(ao_data.format);

    // Master RIFF chunk
    fput32le(WAV_ID_RIFF, fp);
    // RIFF chunk size: 'WAVE' + 'fmt ' + 4 + fmt_chunk_size + data chunk hdr (8) + data length
    fput32le(12 + fmt_chunk_size + 8 + data_length, fp);
    fput32le(WAV_ID_WAVE, fp);

    // Format chunk
    fput32le(WAV_ID_FMT, fp);
    fput32le(fmt_chunk_size, fp);
    fput16le(use_waveex ? WAV_ID_FORMAT_EXTENSIBLE : fmt, fp);
    fput16le(ao_data.channels, fp);
    fput32le(ao_data.samplerate, fp);
    fput32le(ao_data.bps, fp);
    fput16le(ao_data.channels * (bits / 8), fp);
    fput16le(bits, fp);

    if (use_waveex) {
        // Extension chunk
        fput16le(22, fp);
        fput16le(bits, fp);
        switch (ao_data.channels) {
            case 5:
                fput32le(0x0607, fp); // L R C Lb Rb
                break;
            case 6:
                fput32le(0x060f, fp); // L R C Lb Rb LFE
                break;
            case 7:
                fput32le(0x0727, fp); // L R C Cb Ls Rs LFE
                break;
            case 8:
                fput32le(0x063f, fp); // L R C Lb Rb Ls Rs LFE
                break;
        }
        // 2 bytes format + 14 bytes guid
        fput32le(fmt, fp);
        fput32le(0x00100000, fp);
        fput32le(0xAA000080, fp);
        fput32le(0x719B3800, fp);
    }

    // Data chunk
    fput32le(WAV_ID_DATA, fp);
    fput32le(data_length, fp);
}

// to set/get/query special features/parameters
static int control(int cmd,void *arg){
    return -1;
}

// open & setup audio device
// return: 1=success 0=fail
static int init(int rate,int channels,int format,int flags){
    const opt_t subopts[] = {
        {"waveheader", OPT_ARG_BOOL, &ao_pcm_waveheader, NULL},
        {"file",       OPT_ARG_MSTRZ, &ao_outputfilename, NULL},
        {"fast",       OPT_ARG_BOOL, &fast, NULL},
        {NULL}
    };
    // set defaults
    ao_pcm_waveheader = 1;

    if (subopt_parse(ao_subdevice, subopts) != 0) {
        return 0;
    }
    if (!ao_outputfilename){
        ao_outputfilename =
            strdup(ao_pcm_waveheader?"audiodump.wav":"audiodump.pcm");
    }

    if (ao_pcm_waveheader)
    {
        // WAV files must have one of the following formats

        switch(format){
        case AF_FORMAT_U8:
        case AF_FORMAT_S16_LE:
        case AF_FORMAT_S24_LE:
        case AF_FORMAT_S32_LE:
        case AF_FORMAT_FLOAT_LE:
        case AF_FORMAT_AC3_BE:
        case AF_FORMAT_AC3_LE:
             break;
        default:
            format = AF_FORMAT_S16_LE;
            break;
        }
    }

    ao_data.outburst = 65536;
    ao_data.buffersize= 2*65536;
    ao_data.channels=channels;
    ao_data.samplerate=rate;
    ao_data.format=format;
    ao_data.bps=channels*rate*(af_fmt2bits(format)/8);

    mp_tmsg(MSGT_AO, MSGL_INFO, "[AO PCM] File: %s (%s)\nPCM: Samplerate: %iHz Channels: %s Format %s\n", ao_outputfilename,
           (ao_pcm_waveheader?"WAVE":"RAW PCM"), rate,
           (channels > 1) ? "Stereo" : "Mono", af_fmt2str_short(format));
    mp_tmsg(MSGT_AO, MSGL_INFO, "[AO PCM] Info: Faster dumping is achieved with -novideo -ao pcm:fast\n[AO PCM] Info: To write WAVE files use -ao pcm:waveheader (default).\n");

    fp = fopen(ao_outputfilename, "wb");
    if(fp) {
        if(ao_pcm_waveheader){ /* Reserve space for wave header */
            write_wave_header(fp, 0x7ffff000);
        }
        return 1;
    }
    mp_tmsg(MSGT_AO, MSGL_ERR, "[AO PCM] Failed to open %s for writing!\n",
               ao_outputfilename);
    return 0;
}

// close audio device
static void uninit(int immed){

    if(ao_pcm_waveheader){ /* Rewrite wave header */
        int broken_seek = 0;
#ifdef __MINGW32__
        // Windows, in its usual idiocy "emulates" seeks on pipes so it always looks
        // like they work. So we have to detect them brute-force.
        broken_seek = GetFileType((HANDLE)_get_osfhandle(_fileno(fp))) != FILE_TYPE_DISK;
#endif
        if (broken_seek || fseek(fp, 0, SEEK_SET) != 0)
            mp_msg(MSGT_AO, MSGL_ERR, "Could not seek to start, WAV size headers not updated!\n");
        else {
            if (data_length > 0xfffff000) {
                mp_msg(MSGT_AO, MSGL_ERR, "File larger than allowed for WAV files, may play truncated!\n");
                data_length = 0xfffff000;
            }
            write_wave_header(fp, data_length);
        }
    }
    fclose(fp);
    free(ao_outputfilename);
    ao_outputfilename = NULL;
}

// stop playing and empty buffers (for seeking/pause)
static void reset(void){

}

// stop playing, keep buffers (for pause)
static void audio_pause(void)
{
    // for now, just call reset();
    reset();
}

// resume playing, after audio_pause()
static void audio_resume(void)
{
}

// return: how many bytes can be played without blocking
static int get_space(void){

    if(vo_pts)
        return ao_data.pts < vo_pts + fast * 30000 ? ao_data.outburst : 0;
    return ao_data.outburst;
}

// plays 'len' bytes of 'data'
// it should round it down to outburst*n
// return: number of bytes played
static int play(void* data,int len,int flags){

    if (ao_data.channels == 5 || ao_data.channels == 6 || ao_data.channels == 8) {
        int frame_size = af_fmt2bits(ao_data.format) / 8;
        len -= len % (frame_size * ao_data.channels);
        reorder_channel_nch(data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
                            AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
                            ao_data.channels,
                            len / frame_size, frame_size);
    }

    //printf("PCM: Writing chunk!\n");
    fwrite(data,len,1,fp);

    if(ao_pcm_waveheader)
        data_length += len;

    return len;
}

// return: delay in seconds between first and last sample in buffer
static float get_delay(void){

    return 0.0;
}