2009-01-26 15:06:44 +00:00
|
|
|
/*
|
|
|
|
* PCM audio output driver
|
|
|
|
*
|
2013-10-23 17:07:27 +00:00
|
|
|
* Original author: Atmosfear
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-05-20 10:41:33 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-01-26 15:06:44 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-05-20 10:41:33 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2017-05-20 10:41:33 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-26 15:06:44 +00:00
|
|
|
*/
|
|
|
|
|
2002-05-30 11:53:51 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-06-12 11:00:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-05-30 11:53:51 +00:00
|
|
|
#include <string.h>
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2011-05-05 18:34:17 +00:00
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-09-23 19:04:37 +00:00
|
|
|
#include "osdep/endian.h"
|
2004-09-18 20:31:28 +00:00
|
|
|
|
2009-05-16 13:59:53 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
// for GetFileType to detect pipes
|
|
|
|
#include <windows.h>
|
2011-04-29 13:05:49 +00:00
|
|
|
#include <io.h>
|
2009-05-16 13:59:53 +00:00
|
|
|
#endif
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
struct priv {
|
|
|
|
char *outputfilename;
|
|
|
|
int waveheader;
|
2015-01-14 21:14:56 +00:00
|
|
|
int append;
|
2011-05-05 18:34:17 +00:00
|
|
|
uint64_t data_length;
|
|
|
|
FILE *fp;
|
2001-06-12 11:00:15 +00:00
|
|
|
};
|
|
|
|
|
2001-06-12 14:24:26 +00:00
|
|
|
#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
|
2008-05-01 16:58:52 +00:00
|
|
|
#define WAV_ID_FLOAT_PCM 0x0003
|
2010-02-22 14:24:53 +00:00
|
|
|
#define WAV_ID_FORMAT_EXTENSIBLE 0xfffe
|
2001-06-12 14:24:26 +00:00
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
static void fput16le(uint16_t val, FILE *fp)
|
|
|
|
{
|
2010-02-22 14:24:53 +00:00
|
|
|
uint8_t bytes[2] = {val, val >> 8};
|
|
|
|
fwrite(bytes, 1, 2, fp);
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
static void fput32le(uint32_t val, FILE *fp)
|
|
|
|
{
|
2010-02-22 14:24:53 +00:00
|
|
|
uint8_t bytes[4] = {val, val >> 8, val >> 16, val >> 24};
|
|
|
|
fwrite(bytes, 1, 4, fp);
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
static void write_wave_header(struct ao *ao, FILE *fp, uint64_t data_length)
|
|
|
|
{
|
2014-09-23 19:04:37 +00:00
|
|
|
uint16_t fmt = ao->format == AF_FORMAT_FLOAT ? WAV_ID_FLOAT_PCM : WAV_ID_PCM;
|
2015-06-26 21:06:37 +00:00
|
|
|
int bits = af_fmt_to_bytes(ao->format) * 8;
|
2010-02-22 14:24:53 +00:00
|
|
|
|
|
|
|
// Master RIFF chunk
|
|
|
|
fput32le(WAV_ID_RIFF, fp);
|
2014-11-21 09:06:12 +00:00
|
|
|
// RIFF chunk size: 'WAVE' + 'fmt ' + 4 + 40 +
|
2011-05-05 18:34:17 +00:00
|
|
|
// data chunk hdr (8) + data length
|
2014-11-21 09:06:12 +00:00
|
|
|
fput32le(12 + 40 + 8 + data_length, fp);
|
2010-02-22 14:24:53 +00:00
|
|
|
fput32le(WAV_ID_WAVE, fp);
|
|
|
|
|
|
|
|
// Format chunk
|
|
|
|
fput32le(WAV_ID_FMT, fp);
|
2014-11-21 09:06:12 +00:00
|
|
|
fput32le(40, fp);
|
|
|
|
fput16le(WAV_ID_FORMAT_EXTENSIBLE, fp);
|
2013-04-05 21:06:22 +00:00
|
|
|
fput16le(ao->channels.num, fp);
|
2011-05-05 18:34:17 +00:00
|
|
|
fput32le(ao->samplerate, fp);
|
|
|
|
fput32le(ao->bps, fp);
|
2013-04-05 21:06:22 +00:00
|
|
|
fput16le(ao->channels.num * (bits / 8), fp);
|
2010-02-22 14:24:53 +00:00
|
|
|
fput16le(bits, fp);
|
|
|
|
|
2014-11-21 09:06:12 +00:00
|
|
|
// Extension chunk
|
|
|
|
fput16le(22, fp);
|
|
|
|
fput16le(bits, fp);
|
|
|
|
fput32le(mp_chmap_to_waveext(&ao->channels), fp);
|
|
|
|
// 2 bytes format + 14 bytes guid
|
|
|
|
fput32le(fmt, fp);
|
|
|
|
fput32le(0x00100000, fp);
|
|
|
|
fput32le(0xAA000080, fp);
|
|
|
|
fput32le(0x719B3800, fp);
|
2010-02-22 14:24:53 +00:00
|
|
|
|
|
|
|
// Data chunk
|
|
|
|
fput32le(WAV_ID_DATA, fp);
|
|
|
|
fput32le(data_length, fp);
|
|
|
|
}
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2011-05-05 18:34:17 +00:00
|
|
|
{
|
2013-07-21 19:51:03 +00:00
|
|
|
struct priv *priv = ao->priv;
|
2011-05-05 18:34:17 +00:00
|
|
|
|
2020-03-14 12:36:27 +00:00
|
|
|
char *outputfilename = priv->outputfilename;
|
|
|
|
if (!outputfilename) {
|
|
|
|
outputfilename = talloc_strdup(priv, priv->waveheader ? "audiodump.wav"
|
|
|
|
: "audiodump.pcm");
|
|
|
|
}
|
2013-11-10 22:15:02 +00:00
|
|
|
|
|
|
|
ao->format = af_fmt_from_planar(ao->format);
|
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
if (priv->waveheader) {
|
2010-01-23 11:21:56 +00:00
|
|
|
// WAV files must have one of the following formats
|
|
|
|
|
2014-09-23 19:04:37 +00:00
|
|
|
// And they don't work in big endian; fixing it would be simple, but
|
|
|
|
// nobody cares.
|
|
|
|
if (BYTE_ORDER == BIG_ENDIAN) {
|
|
|
|
MP_FATAL(ao, "Not supported on big endian.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
switch (ao->format) {
|
2010-01-23 11:21:56 +00:00
|
|
|
case AF_FORMAT_U8:
|
2014-09-23 19:04:37 +00:00
|
|
|
case AF_FORMAT_S16:
|
|
|
|
case AF_FORMAT_S32:
|
|
|
|
case AF_FORMAT_FLOAT:
|
2010-01-23 11:21:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-26 21:06:37 +00:00
|
|
|
if (!af_fmt_is_spdif(ao->format))
|
audio: cleanup spdif format definitions
Before this commit, there was AF_FORMAT_AC3 (the original spdif format,
used for AC3 and DTS core), and AF_FORMAT_IEC61937 (used for AC3, DTS
and DTS-HD), which was handled as some sort of superset for
AF_FORMAT_AC3. There also was AF_FORMAT_MPEG2, which used
IEC61937-framing, but still was handled as something "separate".
Technically, all of them are pretty similar, but may use different
bitrates. Since digital passthrough pretends to be PCM (just with
special headers that wrap digital packets), this is easily detectable by
the higher samplerate or higher number of channels, so I don't know why
you'd need a separate "class" of sample formats (AF_FORMAT_AC3 vs.
AF_FORMAT_IEC61937) to distinguish them. Actually, this whole thing is
just a mess.
Simplify this by handling all these formats the same way.
AF_FORMAT_IS_IEC61937() now returns 1 for all spdif formats (even MP3).
All AOs just accept all spdif formats now - whether that works or not is
not really clear (seems inconsistent due to earlier attempts to make
DTS-HD work). But on the other hand, enabling spdif requires manual user
interaction, so it doesn't matter much if initialization fails in
slightly less graceful ways if it can't work at all.
At a later point, we will support passthrough with ao_pulse. It seems
the PulseAudio API wants to know the codec type (or maybe not - feeding
it DTS while telling it it's AC3 works), add separate formats for each
codecs. While this reminds of the earlier chaos, it's stricter, and most
code just uses AF_FORMAT_IS_IEC61937().
Also, modify AF_FORMAT_TYPE_MASK (renamed from AF_FORMAT_POINT_MASK) to
include special formats, so that it always describes the fundamental
sample format type. This also ensures valid AF formats are never 0 (this
was probably broken in one of the earlier commits from today).
2014-09-23 20:44:54 +00:00
|
|
|
ao->format = AF_FORMAT_S16;
|
2010-01-23 11:21:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-22 14:27:10 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
mp_chmap_sel_add_waveext(&sel);
|
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
|
|
|
return -1;
|
2013-04-05 21:06:22 +00:00
|
|
|
|
2015-06-26 21:06:37 +00:00
|
|
|
ao->bps = ao->channels.num * ao->samplerate * af_fmt_to_bytes(ao->format);
|
2011-05-05 18:34:17 +00:00
|
|
|
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_INFO(ao, "File: %s (%s)\nPCM: Samplerate: %d Hz Channels: %d Format: %s\n",
|
2020-03-14 12:36:27 +00:00
|
|
|
outputfilename,
|
2011-05-05 18:34:17 +00:00
|
|
|
priv->waveheader ? "WAVE" : "RAW PCM", ao->samplerate,
|
2013-11-07 21:12:36 +00:00
|
|
|
ao->channels.num, af_fmt_to_str(ao->format));
|
2011-05-05 18:34:17 +00:00
|
|
|
|
2020-03-14 12:36:27 +00:00
|
|
|
priv->fp = fopen(outputfilename, priv->append ? "ab" : "wb");
|
2011-05-05 18:34:17 +00:00
|
|
|
if (!priv->fp) {
|
2020-03-14 12:36:27 +00:00
|
|
|
MP_ERR(ao, "Failed to open %s for writing!\n", outputfilename);
|
2011-05-05 18:34:17 +00:00
|
|
|
return -1;
|
2009-03-22 14:27:10 +00:00
|
|
|
}
|
2011-05-05 18:34:17 +00:00
|
|
|
if (priv->waveheader) // Reserve space for wave header
|
|
|
|
write_wave_header(ao, priv->fp, 0x7ffff000);
|
|
|
|
ao->untimed = true;
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
ao->device_buffer = 1 << 16;
|
2011-05-05 18:34:17 +00:00
|
|
|
|
2009-03-22 14:27:10 +00:00
|
|
|
return 0;
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2011-05-05 18:34:17 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
if (priv->waveheader) { // Rewrite wave header
|
|
|
|
bool broken_seek = false;
|
2009-05-16 13:59:53 +00:00
|
|
|
#ifdef __MINGW32__
|
2011-05-05 18:34:17 +00:00
|
|
|
// 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 = FILE_TYPE_DISK !=
|
|
|
|
GetFileType((HANDLE)_get_osfhandle(_fileno(priv->fp)));
|
2009-05-16 13:59:53 +00:00
|
|
|
#endif
|
2011-05-05 18:34:17 +00:00
|
|
|
if (broken_seek || fseek(priv->fp, 0, SEEK_SET) != 0)
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "Could not seek to start, WAV size headers not updated!\n");
|
2009-03-22 14:27:10 +00:00
|
|
|
else {
|
2011-05-05 18:34:17 +00:00
|
|
|
if (priv->data_length > 0xfffff000) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_ERR(ao, "File larger than allowed for "
|
2011-05-05 18:34:17 +00:00
|
|
|
"WAV files, may play truncated!\n");
|
|
|
|
priv->data_length = 0xfffff000;
|
2010-05-21 17:19:01 +00:00
|
|
|
}
|
2011-05-05 18:34:17 +00:00
|
|
|
write_wave_header(ao, priv->fp, priv->data_length);
|
2009-03-22 14:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-05 18:34:17 +00:00
|
|
|
fclose(priv->fp);
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
static bool audio_write(struct ao *ao, void **data, int samples)
|
2001-06-12 11:00:15 +00:00
|
|
|
{
|
2011-05-05 18:34:17 +00:00
|
|
|
struct priv *priv = ao->priv;
|
2013-11-10 22:24:21 +00:00
|
|
|
int len = samples * ao->sstride;
|
2001-06-12 11:00:15 +00:00
|
|
|
|
2013-11-10 22:24:21 +00:00
|
|
|
fwrite(data[0], len, 1, priv->fp);
|
2011-05-05 18:34:17 +00:00
|
|
|
priv->data_length += len;
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_state(struct ao *ao, struct mp_pcm_state *state)
|
|
|
|
{
|
|
|
|
state->free_samples = ao->device_buffer;
|
|
|
|
state->queued_samples = 0;
|
|
|
|
state->delay = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool set_pause(struct ao *ao, bool paused)
|
|
|
|
{
|
|
|
|
return true; // signal support so common code doesn't write silence
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start(struct ao *ao)
|
|
|
|
{
|
|
|
|
// we use data immediately
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
2001-06-12 11:00:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-21 19:51:03 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2011-05-05 18:34:17 +00:00
|
|
|
const struct ao_driver audio_out_pcm = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "RAW PCM/WAVE file writer audio output",
|
|
|
|
.name = "pcm",
|
2011-05-05 18:34:17 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
.get_state = get_state,
|
|
|
|
.set_pause = set_pause,
|
|
|
|
.write = audio_write,
|
|
|
|
.start = start,
|
|
|
|
.reset = reset,
|
2013-07-21 19:51:03 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.priv_defaults = &(const struct priv) { .waveheader = 1 },
|
|
|
|
.options = (const struct m_option[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"file", OPT_STRING(outputfilename), .flags = M_OPT_FILE},
|
|
|
|
{"waveheader", OPT_FLAG(waveheader)},
|
|
|
|
{"append", OPT_FLAG(append)},
|
2013-07-21 19:51:03 +00:00
|
|
|
{0}
|
|
|
|
},
|
2016-11-25 20:00:39 +00:00
|
|
|
.options_prefix = "ao-pcm",
|
2011-05-05 18:34:17 +00:00
|
|
|
};
|