summaryrefslogtreecommitdiffstats
path: root/player
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2017-11-29 21:30:10 +0100
committerwm4 <wm4@nowhere>2017-11-29 21:30:51 +0100
commitd725630b5f5817287d44cb31c3c7b9d815c187db (patch)
tree6c54b58b8bb3a4bfc25e0eafb253266a3a10f766 /player
parent9c909fbb4d4abeabe9ac6731cb0c3f966dacf0ff (diff)
downloadmpv-d725630b5f5817287d44cb31c3c7b9d815c187db.tar.bz2
mpv-d725630b5f5817287d44cb31c3c7b9d815c187db.tar.xz
audio: add audio softvol processing to AO
This does what af_volume used to do. Since we couldn't relicense it, just rewrite it. Since we don't have a new filter mechanism yet, and the libavfilter is too inconvenient, do applying the volume gain in ao.c directly. This is done before handling the audio data to the driver. Since push.c runs a separate thread, and pull.c is called asynchronously from the audio driver's thread, the volume value needs to be synchronized. There's no existing central mutex, so do some shit with atomics. Since there's no atomic_float type predefined (which is at least needed when using the legacy wrapper), do some nonsense about reinterpret casting the float value to an int for the purpose of atomic access. Not sure if using memcpy() is undefined behavior, but for now I don't care. The advantage of not using a filter is lower complexity (no filter auto insertion), and lower latency (gain processing is done after our internal audio buffer of at least 200ms). Disavdantages include inability to use native volume control _before_ other filters with custom filter chains, and the need to add new processing for each new sample type. Since this doesn't reuse any of the old GPL code, nor does indirectly rely on it, volume and replaygain handling now works in LGPL mode. How to process the gain is inspired by libavfilter's af_volume (LGPL). In particular, we use exactly the same rounding, and we quantize processing for integer sample types by 256 steps. Some of libavfilter's copyright may or may not apply, but I think not, and it's the same license anyway.
Diffstat (limited to 'player')
-rw-r--r--player/audio.c115
1 files changed, 57 insertions, 58 deletions
diff --git a/player/audio.c b/player/audio.c
index e08259b1b5..e92d97ffd4 100644
--- a/player/audio.c
+++ b/player/audio.c
@@ -128,6 +128,57 @@ fail:
mp_notify(mpctx, MP_EVENT_CHANGE_ALL, NULL);
}
+static int recreate_audio_filters(struct MPContext *mpctx)
+{
+ assert(mpctx->ao_chain);
+
+ struct af_stream *afs = mpctx->ao_chain->af;
+ if (afs->initialized < 1 && af_init(afs) < 0)
+ goto fail;
+
+ recreate_speed_filters(mpctx);
+ if (afs->initialized < 1 && af_init(afs) < 0)
+ goto fail;
+
+ if (mpctx->opts->softvol == SOFTVOL_NO)
+ MP_ERR(mpctx, "--softvol=no is not supported anymore.\n");
+
+ mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
+
+ return 0;
+
+fail:
+ MP_ERR(mpctx, "Couldn't find matching filter/ao format!\n");
+ return -1;
+}
+
+int reinit_audio_filters(struct MPContext *mpctx)
+{
+ struct ao_chain *ao_c = mpctx->ao_chain;
+ if (!ao_c)
+ return 0;
+
+ double delay = 0;
+ if (ao_c->af->initialized > 0)
+ delay = af_calc_delay(ao_c->af);
+
+ af_uninit(ao_c->af);
+ if (recreate_audio_filters(mpctx) < 0)
+ return -1;
+
+ // Only force refresh if the amount of dropped buffered data is going to
+ // cause "issues" for the A/V sync logic.
+ if (mpctx->audio_status == STATUS_PLAYING && delay > 0.2)
+ issue_refresh_seek(mpctx, MPSEEK_EXACT);
+ return 1;
+}
+
+#else /* HAVE_LIBAV */
+
+int reinit_audio_filters(struct MPContext *mpctx) { return 0; }
+
+#endif /* else HAVE_LIBAF */
+
static double db_gain(double db)
{
return pow(10.0, db/20.0);
@@ -136,11 +187,13 @@ static double db_gain(double db)
static float compute_replaygain(struct MPContext *mpctx)
{
struct MPOpts *opts = mpctx->opts;
- struct ao_chain *ao_c = mpctx->ao_chain;
float rgain = 1.0;
- struct replaygain_data *rg = ao_c->af->replaygain_data;
+ struct replaygain_data *rg = NULL;
+ struct track *track = mpctx->current_track[0][STREAM_AUDIO];
+ if (track)
+ rg = track->stream->codec->replaygain_data;
if (opts->rgain_mode && rg) {
MP_VERBOSE(mpctx, "Replaygain: Track=%f/%f Album=%f/%f\n",
rg->track_gain, rg->track_peak,
@@ -177,7 +230,7 @@ void audio_update_volume(struct MPContext *mpctx)
{
struct MPOpts *opts = mpctx->opts;
struct ao_chain *ao_c = mpctx->ao_chain;
- if (!ao_c || ao_c->af->initialized < 1)
+ if (!ao_c || !ao_c->ao)
return;
float gain = MPMAX(opts->softvol_volume / 100.0, 0);
@@ -185,62 +238,10 @@ void audio_update_volume(struct MPContext *mpctx)
gain *= compute_replaygain(mpctx);
if (opts->softvol_mute == 1)
gain = 0.0;
-}
-
-static int recreate_audio_filters(struct MPContext *mpctx)
-{
- assert(mpctx->ao_chain);
-
- struct af_stream *afs = mpctx->ao_chain->af;
- if (afs->initialized < 1 && af_init(afs) < 0)
- goto fail;
-
- recreate_speed_filters(mpctx);
- if (afs->initialized < 1 && af_init(afs) < 0)
- goto fail;
- if (mpctx->opts->softvol == SOFTVOL_NO)
- MP_ERR(mpctx, "--softvol=no is not supported anymore.\n");
-
- audio_update_volume(mpctx);
-
- mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
-
- return 0;
-
-fail:
- MP_ERR(mpctx, "Couldn't find matching filter/ao format!\n");
- return -1;
+ ao_set_gain(ao_c->ao, gain);
}
-int reinit_audio_filters(struct MPContext *mpctx)
-{
- struct ao_chain *ao_c = mpctx->ao_chain;
- if (!ao_c)
- return 0;
-
- double delay = 0;
- if (ao_c->af->initialized > 0)
- delay = af_calc_delay(ao_c->af);
-
- af_uninit(ao_c->af);
- if (recreate_audio_filters(mpctx) < 0)
- return -1;
-
- // Only force refresh if the amount of dropped buffered data is going to
- // cause "issues" for the A/V sync logic.
- if (mpctx->audio_status == STATUS_PLAYING && delay > 0.2)
- issue_refresh_seek(mpctx, MPSEEK_EXACT);
- return 1;
-}
-
-#else /* HAVE_LIBAV */
-
-void audio_update_volume(struct MPContext *mpctx) {}
-int reinit_audio_filters(struct MPContext *mpctx) { return 0; }
-
-#endif /* else HAVE_LIBAF */
-
// Call this if opts->playback_speed or mpctx->speed_factor_* change.
void update_playback_speed(struct MPContext *mpctx)
{
@@ -603,8 +604,6 @@ void reinit_audio_chain_src(struct MPContext *mpctx, struct track *track)
ao_c->log = mpctx->log;
#if HAVE_LIBAF
ao_c->af = af_new(mpctx->global);
- if (track && track->stream)
- ao_c->af->replaygain_data = track->stream->codec->replaygain_data;
#else
ao_c->conv = mp_aconverter_create(mpctx->global, mpctx->log, NULL);
#endif