1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-22 06:42:03 +00:00

vo/ao: SDL 1.2+ audio driver, SDL 2.0+ accelerated video driver

This mainly serves as a fallback for platforms where nothing better is
available; also as a debugging help. Both the audio and video driver are
not first class - the audio driver lacks delay detection, and the video
driver only supports a single YUV color space.

Configure options: --disable-sdl2 to disable SDL 2.0+ detection,
--disable-sdl to disable SDL 1.2+ detection. Both options need to be
specified to turn off SDL support entirely.
This commit is contained in:
Rudolf Polzer 2012-12-28 08:07:14 +01:00
parent e830b00458
commit c3cc38e4c4
8 changed files with 1458 additions and 0 deletions

View File

@ -92,6 +92,21 @@ dsound (Windows only)
Sets the device number to use. Playing a file with ``-v`` will show a
list of available devices.
sdl
SDL 1.2+ audio output driver. Should work everywhere where SDL 1.2 builds,
but may require the SDL_AUDIODRIVER environment variable to be set
appropriately for your system.
buflen=<length>
Sets the audio buffer length in seconds. Is used only approximately,
or even disaregarded entirely by the sound system. Playing a file with
``-v`` will show the requested and obtained exact buffer size. A value
of 0 selects the sound system default.
bufcnt=<count>
Sets the number of extra audio buffers in mpv. Usually needs not be
changed.
null
Produces no audio output but maintains video playback speed. Use
``--no-audio`` for benchmarking.

View File

@ -660,6 +660,11 @@ opengl-old
x11
X11/GLX
sdl
SDL 2.0+ Render video output driver, depending on system with or without
hardware acceleration. Should work everywhere where SDL 2.0 builds. For
tuning, refer to your copy of the file SDL_hints.h.
null
Produces no video output. Useful for benchmarking.

View File

@ -89,6 +89,8 @@ SOURCES-$(ALSA) += audio/out/ao_alsa.c
SOURCES-$(APPLE_IR) += core/input/appleir.c
SOURCES-$(APPLE_REMOTE) += core/input/ar.c
SOURCES-$(CACA) += video/out/vo_caca.c
SOURCES-$(SDL) += audio/out/ao_sdl.c
SOURCES-$(SDL2) += video/out/vo_sdl.c
SOURCES-$(COREAUDIO) += audio/out/ao_coreaudio.c
SOURCES-$(COREVIDEO) += video/out/vo_corevideo.m
SOURCES-$(DIRECT3D) += video/out/vo_direct3d.c \

View File

@ -45,6 +45,7 @@ extern const struct ao_driver audio_out_pcm;
extern const struct ao_driver audio_out_pss;
extern const struct ao_driver audio_out_lavc;
extern const struct ao_driver audio_out_portaudio;
extern const struct ao_driver audio_out_sdl;
static const struct ao_driver * const audio_out_drivers[] = {
// native:
@ -72,6 +73,9 @@ static const struct ao_driver * const audio_out_drivers[] = {
#endif
#ifdef CONFIG_OPENAL
&audio_out_openal,
#endif
#ifdef CONFIG_SDL
&audio_out_sdl,
#endif
&audio_out_null,
// should not be auto-selected:

379
audio/out/ao_sdl.c Normal file
View File

@ -0,0 +1,379 @@
/*
* SDL2 audio output
* Copyright (C) 2012 Rudolf Polzer <divVerent@xonotic.org>
*
* 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 "audio/format.h"
#include "talloc.h"
#include "ao.h"
#include "core/mp_msg.h"
#include "core/subopt-helper.h"
#include "osdep/timer.h"
#include <libavutil/fifo.h>
#include <SDL.h>
// hack because SDL can't be asked about the current delay
#define ESTIMATE_DELAY
struct priv
{
AVFifoBuffer *buffer;
SDL_mutex *buffer_mutex;
SDL_cond *underrun_cond;
bool unpause;
bool paused;
#ifdef ESTIMATE_DELAY
unsigned int callback_time0;
unsigned int callback_time1;
#endif
};
static void audio_callback(void *userdata, Uint8 *stream, int len)
{
struct ao *ao = userdata;
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
#ifdef ESTIMATE_DELAY
priv->callback_time1 = priv->callback_time0;
priv->callback_time0 = GetTimer();
#endif
while (len > 0 && !priv->paused) {
int got = av_fifo_size(priv->buffer);
if (got > len)
got = len;
if (got > 0) {
av_fifo_generic_read(priv->buffer, stream, got, NULL);
len -= got;
stream += got;
}
if (len > 0)
SDL_CondWait(priv->underrun_cond, priv->buffer_mutex);
}
SDL_UnlockMutex(priv->buffer_mutex);
}
static void uninit(struct ao *ao, bool cut_audio)
{
struct priv *priv = ao->priv;
if (!priv)
return;
// abort the callback
priv->paused = 1;
if (SDL_WasInit(SDL_INIT_AUDIO)) {
if (priv->buffer_mutex)
SDL_LockMutex(priv->buffer_mutex);
if (priv->underrun_cond)
SDL_CondSignal(priv->underrun_cond);
if (priv->buffer_mutex)
SDL_UnlockMutex(priv->buffer_mutex);
// make sure the callback exits
SDL_LockAudio();
// close audio device
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
// get rid of the mutex
if (priv->underrun_cond)
SDL_DestroyCond(priv->underrun_cond);
if (priv->buffer_mutex)
SDL_DestroyMutex(priv->buffer_mutex);
if (priv->buffer)
av_fifo_free(priv->buffer);
talloc_free(ao->priv);
ao->priv = NULL;
}
static unsigned int ceil_power_of_two(unsigned int x)
{
int y = 1;
while (y < x)
y *= 2;
return y;
}
static void print_help(void) {
mp_msg(MSGT_AO, MSGL_FATAL,
"\n-ao sdl commandline help:\n"
"Example: mpv -ao sdl:buflen=len\n"
"\nOptions:\n"
" buflen=len\n"
" Length of audio buffer in seconds\n"
" bufcnt=cnt\n"
" Count of extra audio buffers\n"
);
}
static int init(struct ao *ao, char *params)
{
if (SDL_WasInit(SDL_INIT_AUDIO)) {
mp_msg(MSGT_AO, MSGL_ERR, "[sdl] already initialized\n");
return -1;
}
float buflen = 0; // use SDL default
float bufcnt = 2;
const opt_t subopts[] = {
{"buflen", OPT_ARG_FLOAT, &buflen, NULL},
{"bufcnt", OPT_ARG_FLOAT, &bufcnt, NULL},
{NULL}
};
if (subopt_parse(params, subopts) != 0) {
print_help();
return -1;
}
struct priv *priv = talloc_zero(ao, struct priv);
ao->priv = priv;
if (SDL_InitSubSystem(SDL_INIT_AUDIO)) {
if (!ao->probing)
mp_msg(MSGT_AO, MSGL_ERR, "[sdl] SDL_Init failed\n");
uninit(ao, true);
return -1;
}
SDL_AudioSpec desired, obtained;
int bytes = 0;
switch (ao->format) {
case AF_FORMAT_U8: desired.format = AUDIO_U8; bytes = 1; break;
case AF_FORMAT_S8: desired.format = AUDIO_S8; bytes = 1; break;
case AF_FORMAT_U16_LE: desired.format = AUDIO_U16LSB; bytes = 2; break;
case AF_FORMAT_U16_BE: desired.format = AUDIO_U16MSB; bytes = 2; break;
default:
case AF_FORMAT_S16_LE: desired.format = AUDIO_S16LSB; bytes = 2; break;
case AF_FORMAT_S16_BE: desired.format = AUDIO_S16MSB; bytes = 2; break;
#ifdef AUDIO_S32LSB
case AF_FORMAT_S32_LE: desired.format = AUDIO_S32LSB; bytes = 4; break;
#endif
#ifdef AUDIO_S32MSB
case AF_FORMAT_S32_BE: desired.format = AUDIO_S32MSB; bytes = 4; break;
#endif
#ifdef AUDIO_F32LSB
case AF_FORMAT_FLOAT_LE: desired.format = AUDIO_F32LSB; bytes = 4; break;
#endif
#ifdef AUDIO_F32MSB
case AF_FORMAT_FLOAT_BE: desired.format = AUDIO_F32MSB; bytes = 4; break;
#endif
}
desired.freq = ao->samplerate;
desired.channels = ao->channels;
desired.samples = FFMIN(32768, ceil_power_of_two(ao->samplerate * buflen));
desired.callback = audio_callback;
desired.userdata = ao;
mp_msg(MSGT_AO, MSGL_V, "[sdl] requested format: %d Hz, %d channels, %x, "
"buffer size: %d samples\n",
(int) desired.freq, (int) desired.channels,
(int) desired.format, (int) desired.samples);
obtained = desired;
if (SDL_OpenAudio(&desired, &obtained)) {
if (!ao->probing)
mp_msg(MSGT_AO, MSGL_ERR, "[sdl] could not open audio: %s\n",
SDL_GetError());
uninit(ao, true);
return -1;
}
mp_msg(MSGT_AO, MSGL_V, "[sdl] obtained format: %d Hz, %d channels, %x, "
"buffer size: %d samples\n",
(int) obtained.freq, (int) obtained.channels,
(int) obtained.format, (int) obtained.samples);
switch (obtained.format) {
case AUDIO_U8: ao->format = AF_FORMAT_U8; bytes = 1; break;
case AUDIO_S8: ao->format = AF_FORMAT_S8; bytes = 1; break;
case AUDIO_S16LSB: ao->format = AF_FORMAT_S16_LE; bytes = 2; break;
case AUDIO_S16MSB: ao->format = AF_FORMAT_S16_BE; bytes = 2; break;
case AUDIO_U16LSB: ao->format = AF_FORMAT_U16_LE; bytes = 2; break;
case AUDIO_U16MSB: ao->format = AF_FORMAT_U16_BE; bytes = 2; break;
#ifdef AUDIO_S32LSB
case AUDIO_S32LSB: ao->format = AF_FORMAT_S32_LE; bytes = 4; break;
#endif
#ifdef AUDIO_S32MSB
case AUDIO_S32MSB: ao->format = AF_FORMAT_S32_BE; bytes = 4; break;
#endif
#ifdef AUDIO_F32LSB
case AUDIO_F32LSB: ao->format = AF_FORMAT_FLOAT_LE; bytes = 4; break;
#endif
#ifdef AUDIO_F32MSB
case AUDIO_F32MSB: ao->format = AF_FORMAT_FLOAT_BE; bytes = 4; break;
#endif
default:
if (!ao->probing)
mp_msg(MSGT_AO, MSGL_ERR,
"[sdl] could not find matching format\n");
uninit(ao, true);
return -1;
}
ao->samplerate = obtained.freq;
ao->channels = obtained.channels;
ao->bps = ao->channels * ao->samplerate * bytes;
ao->buffersize = obtained.size * bufcnt;
ao->outburst = obtained.size;
priv->buffer = av_fifo_alloc(ao->buffersize);
priv->buffer_mutex = SDL_CreateMutex();
if (!priv->buffer_mutex) {
mp_msg(MSGT_AO, MSGL_ERR, "[sdl] SDL_CreateMutex failed\n");
uninit(ao, true);
return -1;
}
priv->underrun_cond = SDL_CreateCond();
if (!priv->underrun_cond) {
mp_msg(MSGT_AO, MSGL_ERR, "[sdl] SDL_CreateCond failed\n");
uninit(ao, true);
return -1;
}
priv->unpause = 1;
priv->paused = 1;
priv->callback_time0 = priv->callback_time1 = GetTimer();
return 1;
}
static void reset(struct ao *ao)
{
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
av_fifo_reset(priv->buffer);
SDL_UnlockMutex(priv->buffer_mutex);
}
static int get_space(struct ao *ao)
{
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
int space = av_fifo_space(priv->buffer);
SDL_UnlockMutex(priv->buffer_mutex);
return space;
}
static void pause(struct ao *ao)
{
struct priv *priv = ao->priv;
SDL_PauseAudio(SDL_TRUE);
priv->unpause = 0;
priv->paused = 1;
SDL_CondSignal(priv->underrun_cond);
}
static void do_resume(struct ao *ao)
{
struct priv *priv = ao->priv;
priv->paused = 0;
SDL_PauseAudio(SDL_FALSE);
}
static void resume(struct ao *ao)
{
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
int free = av_fifo_space(priv->buffer);
SDL_UnlockMutex(priv->buffer_mutex);
if (free)
priv->unpause = 1;
else
do_resume(ao);
}
static int play(struct ao *ao, void *data, int len, int flags)
{
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
int free = av_fifo_space(priv->buffer);
if (len > free) len = free;
av_fifo_generic_write(priv->buffer, data, len, NULL);
SDL_CondSignal(priv->underrun_cond);
SDL_UnlockMutex(priv->buffer_mutex);
if (priv->unpause) {
priv->unpause = 0;
do_resume(ao);
}
return len;
}
static float get_delay(struct ao *ao)
{
struct priv *priv = ao->priv;
SDL_LockMutex(priv->buffer_mutex);
int sz = av_fifo_size(priv->buffer);
#ifdef ESTIMATE_DELAY
unsigned int callback_time0 = priv->callback_time0;
unsigned int callback_time1 = priv->callback_time1;
#endif
SDL_UnlockMutex(priv->buffer_mutex);
// delay component: our FIFO's length
float delay = sz / (float) ao->bps;
#ifdef ESTIMATE_DELAY
// delay component: outstanding audio living in SDL
unsigned int current_time = GetTimer();
// interval between callbacks
unsigned int callback_interval = callback_time0 - callback_time1;
unsigned int elapsed_interval = current_time - callback_time0;
if (elapsed_interval > callback_interval)
elapsed_interval = callback_interval;
// delay subcomponent: remaining audio from the currently played buffer
unsigned int buffer_interval = callback_interval - elapsed_interval;
// delay subcomponent: remaining audio from the next played buffer, as
// provided by the callback
buffer_interval += callback_interval;
delay += buffer_interval / 1000000.0;
#endif
return delay;
}
const struct ao_driver audio_out_sdl = {
.is_new = true,
.info = &(const struct ao_info) {
"SDL Audio",
"sdl",
"Rudolf Polzer <divVerent@xonotic.org>",
""
},
.init = init,
.uninit = uninit,
.get_space = get_space,
.play = play,
.get_delay = get_delay,
.pause = pause,
.resume = resume,
.reset = reset,
};

45
configure vendored
View File

@ -348,6 +348,8 @@ Video output:
--enable-gl enable OpenGL video output [autodetect]
--enable-caca enable CACA video output [autodetect]
--enable-direct3d enable Direct3D video output [autodetect]
--enable-sdl enable SDL audio output [autodetect]
--enable-sdl2 enable SDL 2.0+ audio and video output [autodetect]
--enable-xv enable Xv video output [autodetect]
--enable-vdpau enable VDPAU acceleration [autodetect]
--enable-vm enable XF86VidMode support [autodetect]
@ -422,6 +424,8 @@ _xss=auto
_xv=auto
_vdpau=auto
_direct3d=auto
_sdl=auto
_sdl2=auto
_dsound=auto
_nas=auto
_mng=auto
@ -595,6 +599,10 @@ for ac_option do
--disable-vdpau) _vdpau=no ;;
--enable-direct3d) _direct3d=yes ;;
--disable-direct3d) _direct3d=no ;;
--enable-sdl) _sdl=yes ;;
--disable-sdl) _sdl=no ;;
--enable-sdl2) _sdl2=yes ;;
--disable-sdl2) _sdl2=no ;;
--enable-dsound) _dsound=yes ;;
--disable-dsound) _dsound=no ;;
--enable-mng) _mng=yes ;;
@ -2319,6 +2327,39 @@ echores "$_dsound"
fi #if win32; then
echocheck "SDL 2.0"
if test "$_sdl2" = auto ; then
pkg_config_add 'sdl2' && _sdl2=yes
fi
if test "$_sdl2" = yes ; then
_sdl=yes # sdl2 implies sdl
def_sdl='#define CONFIG_SDL 1'
def_sdl2='#define CONFIG_SDL2 1'
vomodules="sdl $vomodules"
aomodules="sdl $aomodules"
echores "$_sdl2"
else
def_sdl2='#undef CONFIG_SDL2'
echores "$_sdl2"
echocheck "SDL"
if test "$_sdl" = auto ; then
pkg_config_add 'sdl' && _sdl=yes
fi
if test "$_sdl" = yes ; then
def_sdl='#define CONFIG_SDL 1'
novomodules="sdl $novomodules"
aomodules="sdl $aomodules"
else
def_sdl='#undef CONFIG_SDL'
novomodules="sdl $novomodules"
noaomodules="sdl $noaomodules"
fi
echores "$_sdl"
fi
#########
# AUDIO #
#########
@ -3159,6 +3200,8 @@ COCOA = $_cocoa
COREAUDIO = $_coreaudio
COREVIDEO = $_corevideo
DIRECT3D = $_direct3d
SDL = $_sdl
SDL2 = $_sdl2
DSOUND = $_dsound
DVBIN = $_dvbin
DVDREAD = $_dvdread
@ -3405,6 +3448,8 @@ $def_caca
$def_corevideo
$def_cocoa
$def_direct3d
$def_sdl
$def_sdl2
$def_dsound
$def_dvb
$def_dvbin

View File

@ -83,6 +83,7 @@ extern struct vo_driver video_out_lavc;
extern struct vo_driver video_out_caca;
extern struct vo_driver video_out_direct3d;
extern struct vo_driver video_out_direct3d_shaders;
extern struct vo_driver video_out_sdl;
extern struct vo_driver video_out_corevideo;
const struct vo_driver *video_out_drivers[] =
@ -110,6 +111,9 @@ const struct vo_driver *video_out_drivers[] =
&video_out_opengl_old,
#endif
#endif
#ifdef CONFIG_SDL2
&video_out_sdl,
#endif
#ifdef CONFIG_X11
&video_out_x11,
#endif

1004
video/out/vo_sdl.c Normal file

File diff suppressed because it is too large Load Diff