Coverage Report

Created: 2025-10-27 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/audio/out/ao_pcm.c
Line
Count
Source
1
/*
2
 * PCM audio output driver
3
 *
4
 * Original author: Atmosfear
5
 *
6
 * This file is part of mpv.
7
 *
8
 * mpv is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * mpv is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include <libavutil/common.h>
27
28
#include "mpv_talloc.h"
29
30
#include "options/m_option.h"
31
#include "options/path.h"
32
#include "audio/format.h"
33
#include "ao.h"
34
#include "internal.h"
35
#include "common/msg.h"
36
#include "osdep/endian.h"
37
38
#ifdef _WIN32
39
// for GetFileType to detect pipes
40
#include <windows.h>
41
#include <io.h>
42
#endif
43
44
struct priv {
45
    char *outputfilename;
46
    bool waveheader;
47
    bool append;
48
    uint64_t data_length;
49
    FILE *fp;
50
};
51
52
2
#define WAV_ID_RIFF 0x46464952 /* "RIFF" */
53
2
#define WAV_ID_WAVE 0x45564157 /* "WAVE" */
54
2
#define WAV_ID_FMT  0x20746d66 /* "fmt " */
55
2
#define WAV_ID_DATA 0x61746164 /* "data" */
56
4
#define WAV_ID_PCM  0x0001
57
0
#define WAV_ID_FLOAT_PCM  0x0003
58
2
#define WAV_ID_FORMAT_EXTENSIBLE 0xfffe
59
60
static void fput16le(uint16_t val, FILE *fp)
61
12
{
62
12
    uint8_t bytes[2] = {val, val >> 8};
63
12
    fwrite(bytes, 1, 2, fp);
64
12
}
65
66
static void fput32le(uint32_t val, FILE *fp)
67
28
{
68
28
    uint8_t bytes[4] = {val, val >> 8, val >> 16, val >> 24};
69
28
    fwrite(bytes, 1, 4, fp);
70
28
}
71
72
static void write_wave_header(struct ao *ao, FILE *fp, uint64_t data_length)
73
2
{
74
2
    uint16_t fmt = ao->format == AF_FORMAT_FLOAT ? WAV_ID_FLOAT_PCM : WAV_ID_PCM;
75
2
    int bits = af_fmt_to_bytes(ao->format) * 8;
76
77
    // Master RIFF chunk
78
2
    fput32le(WAV_ID_RIFF, fp);
79
    // RIFF chunk size: 'WAVE' + 'fmt ' + 4 + 40 +
80
    // data chunk hdr (8) + data length
81
2
    fput32le(12 + 40 + 8 + data_length, fp);
82
2
    fput32le(WAV_ID_WAVE, fp);
83
84
    // Format chunk
85
2
    fput32le(WAV_ID_FMT, fp);
86
2
    fput32le(40, fp);
87
2
    fput16le(WAV_ID_FORMAT_EXTENSIBLE, fp);
88
2
    fput16le(ao->channels.num, fp);
89
2
    fput32le(ao->samplerate, fp);
90
2
    fput32le(MPCLAMP(ao->bps, 0, UINT32_MAX), fp);
91
2
    fput16le(ao->channels.num * (bits / 8), fp);
92
2
    fput16le(bits, fp);
93
94
    // Extension chunk
95
2
    fput16le(22, fp);
96
2
    fput16le(bits, fp);
97
2
    fput32le(mp_chmap_to_waveext(&ao->channels), fp);
98
    // 2 bytes format + 14 bytes guid
99
2
    fput32le(fmt, fp);
100
2
    fput32le(0x00100000, fp);
101
2
    fput32le(0xAA000080, fp);
102
2
    fput32le(0x719B3800, fp);
103
104
    // Data chunk
105
2
    fput32le(WAV_ID_DATA, fp);
106
2
    fput32le(data_length, fp);
107
2
}
108
109
static int init(struct ao *ao)
110
1
{
111
1
    struct priv *priv = ao->priv;
112
113
1
    char *outputfilename = mp_get_user_path(priv, ao->global, priv->outputfilename);
114
1
    if (!outputfilename) {
115
1
        outputfilename = talloc_strdup(priv, priv->waveheader ? "audiodump.wav"
116
1
                                                              : "audiodump.pcm");
117
1
    }
118
119
1
    ao->format = af_fmt_from_planar(ao->format);
120
121
1
    if (priv->waveheader) {
122
        // WAV files must have one of the following formats
123
124
        // And they don't work in big endian; fixing it would be simple, but
125
        // nobody cares.
126
1
        if (BYTE_ORDER == BIG_ENDIAN) {
127
0
            MP_FATAL(ao, "Not supported on big endian.\n");
128
0
            return -1;
129
0
        }
130
131
1
        switch (ao->format) {
132
0
        case AF_FORMAT_U8:
133
1
        case AF_FORMAT_S16:
134
1
        case AF_FORMAT_S32:
135
1
        case AF_FORMAT_FLOAT:
136
1
             break;
137
0
        default:
138
0
            if (!af_fmt_is_spdif(ao->format))
139
0
                ao->format = AF_FORMAT_S16;
140
0
            break;
141
1
        }
142
1
    }
143
144
1
    struct mp_chmap_sel sel = {0};
145
1
    mp_chmap_sel_add_waveext(&sel);
146
1
    if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
147
0
        return -1;
148
149
1
    ao->bps = ao->channels.num * (int64_t)ao->samplerate * af_fmt_to_bytes(ao->format);
150
151
1
    MP_INFO(ao, "File: %s (%s)\nPCM: Samplerate: %d Hz Channels: %d Format: %s\n",
152
1
            outputfilename,
153
1
            priv->waveheader ? "WAVE" : "RAW PCM", ao->samplerate,
154
1
            ao->channels.num, af_fmt_to_str(ao->format));
155
156
1
    priv->fp = fopen(outputfilename, priv->append ? "ab" : "wb");
157
1
    if (!priv->fp) {
158
0
        MP_ERR(ao, "Failed to open %s for writing!\n", outputfilename);
159
0
        return -1;
160
0
    }
161
1
    if (priv->waveheader)  // Reserve space for wave header
162
1
        write_wave_header(ao, priv->fp, 0x7ffff000);
163
1
    ao->untimed = true;
164
1
    ao->device_buffer = 1 << 16;
165
166
1
    return 0;
167
1
}
168
169
// close audio device
170
static void uninit(struct ao *ao)
171
1
{
172
1
    struct priv *priv = ao->priv;
173
174
1
    if (priv->waveheader) {    // Rewrite wave header
175
1
        bool broken_seek = false;
176
#ifdef _WIN32
177
        // Windows, in its usual idiocy "emulates" seeks on pipes so it always
178
        // looks like they work. So we have to detect them brute-force.
179
        broken_seek = FILE_TYPE_DISK !=
180
            GetFileType((HANDLE)_get_osfhandle(_fileno(priv->fp)));
181
#endif
182
1
        if (broken_seek || fseek(priv->fp, 0, SEEK_SET) != 0)
183
1
            MP_ERR(ao, "Could not seek to start, WAV size headers not updated!\n");
184
1
        else {
185
1
            if (priv->data_length > 0xfffff000) {
186
0
                MP_ERR(ao, "File larger than allowed for "
187
0
                       "WAV files, may play truncated!\n");
188
0
                priv->data_length = 0xfffff000;
189
0
            }
190
1
            write_wave_header(ao, priv->fp, priv->data_length);
191
1
        }
192
1
    }
193
1
    fclose(priv->fp);
194
1
}
195
196
static bool audio_write(struct ao *ao, void **data, int samples)
197
1
{
198
1
    struct priv *priv = ao->priv;
199
1
    int len = samples * ao->sstride;
200
201
1
    fwrite(data[0], len, 1, priv->fp);
202
1
    priv->data_length += len;
203
204
1
    return true;
205
1
}
206
207
static void get_state(struct ao *ao, struct mp_pcm_state *state)
208
5
{
209
5
    state->free_samples = ao->device_buffer;
210
5
    state->queued_samples = 0;
211
5
    state->delay = 0;
212
5
}
213
214
static bool set_pause(struct ao *ao, bool paused)
215
0
{
216
0
    return true; // signal support so common code doesn't write silence
217
0
}
218
219
static void start(struct ao *ao)
220
1
{
221
    // we use data immediately
222
1
}
223
224
static void reset(struct ao *ao)
225
0
{
226
0
}
227
228
#define OPT_BASE_STRUCT struct priv
229
230
const struct ao_driver audio_out_pcm = {
231
    .description = "RAW PCM/WAVE file writer audio output",
232
    .name      = "pcm",
233
    .init      = init,
234
    .uninit    = uninit,
235
    .get_state = get_state,
236
    .set_pause = set_pause,
237
    .write     = audio_write,
238
    .start     = start,
239
    .reset     = reset,
240
    .priv_size = sizeof(struct priv),
241
    .priv_defaults = &(const struct priv) { .waveheader = true },
242
    .options = (const struct m_option[]) {
243
        {"file", OPT_STRING(outputfilename), .flags = M_OPT_FILE},
244
        {"waveheader", OPT_BOOL(waveheader)},
245
        {"append", OPT_BOOL(append)},
246
        {0}
247
    },
248
    .options_prefix = "ao-pcm",
249
};