2013-03-09 08:30:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
* Copyright (c) 2013 Stefano Pigozzi <stefano.pigozzi@gmail.com>
|
|
|
|
*
|
2013-10-23 17:05:47 +00:00
|
|
|
* Based on Michael Niedermayer's lavcresample.
|
2013-03-09 08:30:26 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-03-09 08:30:26 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-03-09 08:30:26 +00:00
|
|
|
* 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
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-03-09 08:30:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
2015-03-02 18:09:44 +00:00
|
|
|
#include <math.h>
|
2013-11-10 22:17:04 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-03-09 08:30:26 +00:00
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libavutil/audioconvert.h>
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
#include <libavutil/samplefmt.h>
|
|
|
|
#include <libavutil/mathematics.h>
|
|
|
|
|
2015-03-02 18:09:44 +00:00
|
|
|
#include "common/common.h"
|
2013-03-09 08:30:26 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_LIBAVRESAMPLE
|
2013-03-09 08:30:26 +00:00
|
|
|
#include <libavresample/avresample.h>
|
2013-07-16 11:28:28 +00:00
|
|
|
#elif HAVE_LIBSWRESAMPLE
|
2013-03-09 08:30:26 +00:00
|
|
|
#include <libswresample/swresample.h>
|
|
|
|
#define AVAudioResampleContext SwrContext
|
|
|
|
#define avresample_alloc_context swr_alloc
|
|
|
|
#define avresample_open swr_init
|
|
|
|
#define avresample_close(x) do { } while(0)
|
2013-10-19 11:18:25 +00:00
|
|
|
#define avresample_free swr_free
|
2013-03-09 08:30:26 +00:00
|
|
|
#define avresample_available(x) 0
|
|
|
|
#define avresample_convert(ctx, out, out_planesize, out_samples, in, in_planesize, in_samples) \
|
|
|
|
swr_convert(ctx, out, out_samples, (const uint8_t**)(in), in_samples)
|
2013-04-05 22:56:01 +00:00
|
|
|
#define avresample_set_channel_mapping swr_set_channel_mapping
|
2013-03-09 08:30:26 +00:00
|
|
|
#else
|
2013-07-16 11:28:28 +00:00
|
|
|
#error "config.h broken or no resampler found"
|
2013-03-09 08:30:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-02 01:12:09 +00:00
|
|
|
#include "common/av_common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2013-03-09 08:30:26 +00:00
|
|
|
#include "audio/filter/af.h"
|
2013-03-23 12:01:44 +00:00
|
|
|
#include "audio/fmt-conversion.h"
|
2013-03-09 08:30:26 +00:00
|
|
|
|
|
|
|
struct af_resample_opts {
|
|
|
|
int filter_size;
|
|
|
|
int phase_shift;
|
|
|
|
int linear;
|
|
|
|
double cutoff;
|
|
|
|
|
2015-03-02 18:09:44 +00:00
|
|
|
int in_rate_af; // filter input sample rate
|
|
|
|
int in_rate; // actual rate (used by lavr), adjusted for playback speed
|
2013-03-23 12:01:44 +00:00
|
|
|
int in_format;
|
2013-04-05 18:39:52 +00:00
|
|
|
struct mp_chmap in_channels;
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
int out_rate;
|
|
|
|
int out_format;
|
2013-04-05 18:39:52 +00:00
|
|
|
struct mp_chmap out_channels;
|
2013-03-09 08:30:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct af_resample {
|
2013-03-29 21:29:13 +00:00
|
|
|
int allow_detach;
|
2014-08-02 01:12:09 +00:00
|
|
|
char **avopts;
|
2015-03-02 18:09:44 +00:00
|
|
|
double playback_speed;
|
|
|
|
bool avrctx_ok;
|
2013-03-09 08:30:26 +00:00
|
|
|
struct AVAudioResampleContext *avrctx;
|
2015-05-04 21:59:20 +00:00
|
|
|
struct mp_audio avrctx_fmt; // output format of avrctx
|
2013-04-05 22:56:01 +00:00
|
|
|
struct AVAudioResampleContext *avrctx_out; // for output channel reordering
|
2013-03-09 08:30:26 +00:00
|
|
|
struct af_resample_opts ctx; // opts in the context
|
|
|
|
struct af_resample_opts opts; // opts requested by the user
|
2013-04-05 22:56:01 +00:00
|
|
|
// At least libswresample keeps a pointer around for this:
|
|
|
|
int reorder_in[MP_NUM_CHANNELS];
|
|
|
|
int reorder_out[MP_NUM_CHANNELS];
|
2015-01-14 21:15:31 +00:00
|
|
|
struct mp_audio_pool *reorder_buffer;
|
2013-03-09 08:30:26 +00:00
|
|
|
};
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_LIBAVRESAMPLE
|
2013-03-09 08:30:26 +00:00
|
|
|
static int get_delay(struct af_resample *s)
|
|
|
|
{
|
|
|
|
return avresample_get_delay(s->avrctx);
|
|
|
|
}
|
2013-11-18 13:18:37 +00:00
|
|
|
static void drop_all_output(struct af_resample *s)
|
|
|
|
{
|
|
|
|
while (avresample_read(s->avrctx, NULL, 1000) > 0) {}
|
|
|
|
}
|
2015-06-02 20:20:04 +00:00
|
|
|
static int get_out_samples(struct af_resample *s, int in_samples)
|
2015-03-02 18:09:44 +00:00
|
|
|
{
|
2015-06-02 20:20:04 +00:00
|
|
|
return avresample_get_out_samples(s->avrctx, in_samples);
|
2015-03-02 18:09:44 +00:00
|
|
|
}
|
2013-03-09 08:30:26 +00:00
|
|
|
#else
|
|
|
|
static int get_delay(struct af_resample *s)
|
|
|
|
{
|
|
|
|
return swr_get_delay(s->avrctx, s->ctx.in_rate);
|
|
|
|
}
|
2013-11-18 13:18:37 +00:00
|
|
|
static void drop_all_output(struct af_resample *s)
|
|
|
|
{
|
|
|
|
while (swr_drop_output(s->avrctx, 1000) > 0) {}
|
|
|
|
}
|
2015-06-02 20:20:04 +00:00
|
|
|
static int get_out_samples(struct af_resample *s, int in_samples)
|
2015-03-02 18:09:44 +00:00
|
|
|
{
|
2015-06-04 17:22:45 +00:00
|
|
|
#if LIBSWRESAMPLE_VERSION_MAJOR > 1 || LIBSWRESAMPLE_VERSION_MINOR >= 2
|
|
|
|
return swr_get_out_samples(s->avrctx, in_samples);
|
|
|
|
#else
|
2015-06-04 17:08:40 +00:00
|
|
|
return av_rescale_rnd(in_samples, s->ctx.out_rate, s->ctx.in_rate, AV_ROUND_UP)
|
|
|
|
+ swr_get_delay(s->avrctx, s->ctx.out_rate);
|
2015-06-04 17:22:45 +00:00
|
|
|
#endif
|
2015-03-02 18:09:44 +00:00
|
|
|
}
|
2013-03-09 08:30:26 +00:00
|
|
|
#endif
|
|
|
|
|
2015-03-02 18:09:44 +00:00
|
|
|
static int resample_frame(struct AVAudioResampleContext *r,
|
|
|
|
struct mp_audio *out, struct mp_audio *in)
|
|
|
|
{
|
|
|
|
return avresample_convert(r,
|
|
|
|
out ? (uint8_t **)out->planes : NULL,
|
|
|
|
out ? mp_audio_get_allocated_size(out) : 0,
|
|
|
|
out ? out->samples : 0,
|
|
|
|
in ? (uint8_t **)in->planes : NULL,
|
|
|
|
in ? mp_audio_get_allocated_size(in) : 0,
|
|
|
|
in ? in->samples : 0);
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:30:26 +00:00
|
|
|
static double af_resample_default_cutoff(int filter_size)
|
|
|
|
{
|
|
|
|
return FFMAX(1.0 - 6.5 / (filter_size + 8), 0.80);
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:09:44 +00:00
|
|
|
static int rate_from_speed(int rate, double speed)
|
|
|
|
{
|
|
|
|
return lrint(rate * speed);
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:30:26 +00:00
|
|
|
static bool needs_lavrctx_reconfigure(struct af_resample *s,
|
|
|
|
struct mp_audio *in,
|
|
|
|
struct mp_audio *out)
|
|
|
|
{
|
2015-03-02 18:09:44 +00:00
|
|
|
return s->ctx.in_rate_af != in->rate ||
|
2013-03-23 12:01:44 +00:00
|
|
|
s->ctx.in_format != in->format ||
|
2013-04-05 18:39:52 +00:00
|
|
|
!mp_chmap_equals(&s->ctx.in_channels, &in->channels) ||
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
s->ctx.out_rate != out->rate ||
|
2013-03-23 12:01:44 +00:00
|
|
|
s->ctx.out_format != out->format ||
|
2013-04-05 18:39:52 +00:00
|
|
|
!mp_chmap_equals(&s->ctx.out_channels, &out->channels) ||
|
2013-03-09 08:30:26 +00:00
|
|
|
s->ctx.filter_size != s->opts.filter_size ||
|
|
|
|
s->ctx.phase_shift != s->opts.phase_shift ||
|
|
|
|
s->ctx.linear != s->opts.linear ||
|
|
|
|
s->ctx.cutoff != s->opts.cutoff;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-23 12:05:32 +00:00
|
|
|
static bool test_conversion(int src_format, int dst_format)
|
|
|
|
{
|
|
|
|
return af_to_avformat(src_format) != AV_SAMPLE_FMT_NONE &&
|
|
|
|
af_to_avformat(dst_format) != AV_SAMPLE_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
// mp_chmap_get_reorder() performs:
|
|
|
|
// to->speaker[n] = from->speaker[src[n]]
|
|
|
|
// but libavresample does:
|
|
|
|
// to->speaker[dst[n]] = from->speaker[n]
|
|
|
|
static void transpose_order(int *map, int num)
|
|
|
|
{
|
|
|
|
int nmap[MP_NUM_CHANNELS] = {0};
|
|
|
|
for (int n = 0; n < num; n++) {
|
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
if (map[n] == i)
|
|
|
|
nmap[i] = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(map, nmap, sizeof(nmap));
|
|
|
|
}
|
|
|
|
|
2013-11-09 22:20:58 +00:00
|
|
|
static int configure_lavrr(struct af_instance *af, struct mp_audio *in,
|
|
|
|
struct mp_audio *out)
|
|
|
|
{
|
|
|
|
struct af_resample *s = af->priv;
|
|
|
|
|
2015-03-02 18:09:44 +00:00
|
|
|
s->avrctx_ok = false;
|
|
|
|
|
2013-11-09 22:20:58 +00:00
|
|
|
enum AVSampleFormat in_samplefmt = af_to_avformat(in->format);
|
|
|
|
enum AVSampleFormat out_samplefmt = af_to_avformat(out->format);
|
2015-05-04 21:59:20 +00:00
|
|
|
enum AVSampleFormat out_samplefmtp =
|
|
|
|
af_to_avformat(af_fmt_to_planar(out->format));
|
2013-11-09 22:20:58 +00:00
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
if (in_samplefmt == AV_SAMPLE_FMT_NONE ||
|
|
|
|
out_samplefmt == AV_SAMPLE_FMT_NONE ||
|
|
|
|
out_samplefmtp == AV_SAMPLE_FMT_NONE)
|
2013-11-09 22:20:58 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
|
|
|
|
avresample_close(s->avrctx);
|
|
|
|
avresample_close(s->avrctx_out);
|
|
|
|
|
|
|
|
s->ctx.out_rate = out->rate;
|
2015-03-02 18:09:44 +00:00
|
|
|
s->ctx.in_rate_af = in->rate;
|
|
|
|
s->ctx.in_rate = rate_from_speed(in->rate, s->playback_speed);
|
2013-11-09 22:20:58 +00:00
|
|
|
s->ctx.out_format = out->format;
|
|
|
|
s->ctx.in_format = in->format;
|
|
|
|
s->ctx.out_channels= out->channels;
|
|
|
|
s->ctx.in_channels = in->channels;
|
|
|
|
s->ctx.filter_size = s->opts.filter_size;
|
|
|
|
s->ctx.phase_shift = s->opts.phase_shift;
|
|
|
|
s->ctx.linear = s->opts.linear;
|
|
|
|
s->ctx.cutoff = s->opts.cutoff;
|
|
|
|
|
|
|
|
av_opt_set_int(s->avrctx, "filter_size", s->ctx.filter_size, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "phase_shift", s->ctx.phase_shift, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "linear_interp", s->ctx.linear, 0);
|
|
|
|
|
2013-11-17 15:19:45 +00:00
|
|
|
av_opt_set_double(s->avrctx, "cutoff", s->ctx.cutoff, 0);
|
2013-11-09 22:20:58 +00:00
|
|
|
|
2015-04-01 22:42:54 +00:00
|
|
|
#if HAVE_LIBSWRESAMPLE
|
|
|
|
av_opt_set_double(s->avrctx, "rematrix_maxval", 1.0, 0);
|
|
|
|
#endif
|
|
|
|
|
2014-08-02 01:12:09 +00:00
|
|
|
if (mp_set_avopts(af->log, s->avrctx, s->avopts) < 0)
|
2013-11-09 22:20:58 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
|
|
|
|
struct mp_chmap map_in = in->channels;
|
|
|
|
struct mp_chmap map_out = out->channels;
|
|
|
|
|
|
|
|
// Try not to do any remixing if at least one is "unknown".
|
|
|
|
if (mp_chmap_is_unknown(&map_in) || mp_chmap_is_unknown(&map_out)) {
|
|
|
|
mp_chmap_set_unknown(&map_in, map_in.num);
|
|
|
|
mp_chmap_set_unknown(&map_out, map_out.num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unchecked: don't take any channel reordering into account
|
|
|
|
uint64_t in_ch_layout = mp_chmap_to_lavc_unchecked(&map_in);
|
|
|
|
uint64_t out_ch_layout = mp_chmap_to_lavc_unchecked(&map_out);
|
|
|
|
|
|
|
|
struct mp_chmap in_lavc;
|
|
|
|
mp_chmap_from_lavc(&in_lavc, in_ch_layout);
|
2015-05-04 21:59:20 +00:00
|
|
|
if (in_lavc.num != map_in.num) {
|
|
|
|
// For handling NA channels, we would have to add a planarization step.
|
|
|
|
MP_FATAL(af, "Unsupported channel remapping.\n");
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-11-09 22:20:58 +00:00
|
|
|
mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc);
|
2015-05-04 21:59:20 +00:00
|
|
|
transpose_order(s->reorder_in, map_in.num);
|
2013-11-09 22:20:58 +00:00
|
|
|
|
|
|
|
struct mp_chmap out_lavc;
|
|
|
|
mp_chmap_from_lavc(&out_lavc, out_ch_layout);
|
2015-05-04 21:59:20 +00:00
|
|
|
if (mp_chmap_equals(&out_lavc, &map_out)) {
|
|
|
|
// No intermediate step required - output new format directly.
|
|
|
|
out_samplefmtp = out_samplefmt;
|
|
|
|
} else {
|
|
|
|
// Verify that we really just reorder and/or insert NA channels.
|
|
|
|
struct mp_chmap withna = out_lavc;
|
|
|
|
mp_chmap_fill_na(&withna, map_out.num);
|
|
|
|
if (withna.num != map_out.num)
|
|
|
|
return AF_ERROR;
|
|
|
|
mp_chmap_get_reorder(s->reorder_out, &out_lavc, &map_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->avrctx_fmt = *out;
|
|
|
|
mp_audio_set_channels(&s->avrctx_fmt, &out_lavc);
|
|
|
|
mp_audio_set_format(&s->avrctx_fmt, af_from_avformat(out_samplefmtp));
|
2013-11-09 22:20:58 +00:00
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
// Real conversion; output is input to avrctx_out.
|
|
|
|
av_opt_set_int(s->avrctx, "in_channel_layout", in_ch_layout, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "out_channel_layout", out_ch_layout, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "in_sample_rate", s->ctx.in_rate, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "out_sample_rate", s->ctx.out_rate, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "in_sample_fmt", in_samplefmt, 0);
|
|
|
|
av_opt_set_int(s->avrctx, "out_sample_fmt", out_samplefmtp, 0);
|
|
|
|
|
|
|
|
// Just needs the correct number of channels.
|
|
|
|
int fake_out_ch_layout = av_get_default_channel_layout(map_out.num);
|
|
|
|
if (!fake_out_ch_layout)
|
|
|
|
return AF_ERROR;
|
|
|
|
|
|
|
|
// Deplanarize if needed.
|
|
|
|
av_opt_set_int(s->avrctx_out, "in_channel_layout", fake_out_ch_layout, 0);
|
|
|
|
av_opt_set_int(s->avrctx_out, "out_channel_layout", fake_out_ch_layout, 0);
|
|
|
|
av_opt_set_int(s->avrctx_out, "in_sample_fmt", out_samplefmtp, 0);
|
2013-11-09 22:20:58 +00:00
|
|
|
av_opt_set_int(s->avrctx_out, "out_sample_fmt", out_samplefmt, 0);
|
|
|
|
av_opt_set_int(s->avrctx_out, "in_sample_rate", s->ctx.out_rate, 0);
|
|
|
|
av_opt_set_int(s->avrctx_out, "out_sample_rate", s->ctx.out_rate, 0);
|
|
|
|
|
|
|
|
// API has weird requirements, quoting avresample.h:
|
|
|
|
// * This function can only be called when the allocated context is not open.
|
|
|
|
// * Also, the input channel layout must have already been set.
|
|
|
|
avresample_set_channel_mapping(s->avrctx, s->reorder_in);
|
|
|
|
|
|
|
|
if (avresample_open(s->avrctx) < 0 ||
|
|
|
|
avresample_open(s->avrctx_out) < 0)
|
|
|
|
{
|
2014-08-17 01:29:09 +00:00
|
|
|
MP_ERR(af, "Cannot open Libavresample Context. \n");
|
2013-11-09 22:20:58 +00:00
|
|
|
return AF_ERROR;
|
|
|
|
}
|
2015-03-02 18:09:44 +00:00
|
|
|
s->avrctx_ok = true;
|
2013-11-09 22:20:58 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-09 08:30:26 +00:00
|
|
|
|
|
|
|
static int control(struct af_instance *af, int cmd, void *arg)
|
|
|
|
{
|
2015-04-12 16:04:48 +00:00
|
|
|
struct af_resample *s = af->priv;
|
2013-03-09 08:30:26 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case AF_CONTROL_REINIT: {
|
2015-04-12 16:04:48 +00:00
|
|
|
struct mp_audio *in = arg;
|
|
|
|
struct mp_audio *out = af->data;
|
2013-03-23 12:01:44 +00:00
|
|
|
struct mp_audio orig_in = *in;
|
|
|
|
|
|
|
|
if (((out->rate == in->rate) || (out->rate == 0)) &&
|
|
|
|
(out->format == in->format) &&
|
2013-04-05 18:39:52 +00:00
|
|
|
(mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) &&
|
2015-03-02 18:09:44 +00:00
|
|
|
s->allow_detach && s->playback_speed == 1.0)
|
2013-03-09 08:30:26 +00:00
|
|
|
return AF_DETACH;
|
|
|
|
|
2013-03-23 12:01:44 +00:00
|
|
|
if (out->rate == 0)
|
|
|
|
out->rate = in->rate;
|
|
|
|
|
2013-04-05 18:39:52 +00:00
|
|
|
if (mp_chmap_is_empty(&out->channels))
|
|
|
|
mp_audio_set_channels(out, &in->channels);
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
|
2013-11-09 22:20:58 +00:00
|
|
|
if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE)
|
2013-11-15 20:25:05 +00:00
|
|
|
mp_audio_set_format(in, AF_FORMAT_FLOAT);
|
2013-11-09 22:20:58 +00:00
|
|
|
if (af_to_avformat(out->format) == AV_SAMPLE_FMT_NONE)
|
2013-04-05 17:47:51 +00:00
|
|
|
mp_audio_set_format(out, in->format);
|
2013-03-23 12:01:44 +00:00
|
|
|
|
2013-11-09 22:21:17 +00:00
|
|
|
int r = ((in->format == orig_in.format) &&
|
2013-04-05 18:39:52 +00:00
|
|
|
mp_chmap_equals(&in->channels, &orig_in.channels))
|
2013-11-09 22:21:17 +00:00
|
|
|
? AF_OK : AF_FALSE;
|
|
|
|
|
|
|
|
if (r == AF_OK && needs_lavrctx_reconfigure(s, in, out))
|
|
|
|
r = configure_lavrr(af, in, out);
|
|
|
|
return r;
|
2013-03-23 12:01:44 +00:00
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_FORMAT: {
|
2015-04-12 16:04:04 +00:00
|
|
|
int format = *(int *)arg;
|
|
|
|
if (format && af_to_avformat(format) == AV_SAMPLE_FMT_NONE)
|
2013-03-23 12:01:44 +00:00
|
|
|
return AF_FALSE;
|
|
|
|
|
2015-04-12 16:04:04 +00:00
|
|
|
mp_audio_set_format(af->data, format);
|
2013-03-23 12:01:44 +00:00
|
|
|
return AF_OK;
|
2013-03-09 08:30:26 +00:00
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_CHANNELS: {
|
2013-04-05 18:39:52 +00:00
|
|
|
mp_audio_set_channels(af->data, (struct mp_chmap *)arg);
|
audio: switch to libavcodec channel order, use libavresample for mixing
Switch the internal channel order to libavcodec's. If the channel number
mismatches at some point, use libavresample for up- or downmixing.
Remove the old af_pan automatic downmixing.
The libavcodec channel order should be equivalent to WAVEFORMATEX order,
at least nowadays. reorder_ch.h assumes that WAVEFORMATEX and libavcodec
might be different, but all defined channels have the same mappings.
Remove the downmixing with af_pan as well as the channel conversion with
af_channels from af.c, and prefer af_lavrresample for this. The
automatic downmixing behavior should be the same as before (if the
--channels option is set to 2, which is the default, the audio output
is forced to 2 channels, and libavresample does all downmixing).
Note that mpv still can't do channel layouts. It will pick the default
channel layout according to the channel count. This will be fixed later
by passing down the channel layout as well.
af_hrtf depends on the order of the input channels, so reorder to ALSA
(for which this code was written). This is better than changing the
filter code, which is more risky.
ao_pulse can accept waveext order directly, so set that as channel
mapping.
2013-03-23 16:49:52 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
2013-11-18 13:16:08 +00:00
|
|
|
case AF_CONTROL_SET_RESAMPLE_RATE:
|
2015-04-12 16:04:48 +00:00
|
|
|
af->data->rate = *(int *)arg;
|
2013-03-09 08:30:26 +00:00
|
|
|
return AF_OK;
|
2015-03-02 18:09:44 +00:00
|
|
|
case AF_CONTROL_SET_PLAYBACK_SPEED_RESAMPLE: {
|
|
|
|
s->playback_speed = *(double *)arg;
|
|
|
|
int new_rate = rate_from_speed(s->ctx.in_rate_af, s->playback_speed);
|
|
|
|
if (new_rate != s->ctx.in_rate && s->avrctx_ok && af->fmt_out.format) {
|
|
|
|
// Before reconfiguring, drain the audio that is still buffered
|
|
|
|
// in the resampler.
|
2015-06-02 18:30:30 +00:00
|
|
|
af->filter_frame(af, NULL);
|
2015-03-02 18:09:44 +00:00
|
|
|
// Reinitialize resampler.
|
|
|
|
configure_lavrr(af, &af->fmt_in, &af->fmt_out);
|
|
|
|
}
|
|
|
|
return AF_OK;
|
|
|
|
}
|
2013-11-18 13:18:37 +00:00
|
|
|
case AF_CONTROL_RESET:
|
|
|
|
drop_all_output(s);
|
|
|
|
return AF_OK;
|
2013-03-09 08:30:26 +00:00
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct af_instance *af)
|
|
|
|
{
|
2013-07-22 12:47:12 +00:00
|
|
|
struct af_resample *s = af->priv;
|
|
|
|
if (s->avrctx)
|
|
|
|
avresample_close(s->avrctx);
|
2013-10-19 11:18:25 +00:00
|
|
|
avresample_free(&s->avrctx);
|
2013-07-22 12:47:12 +00:00
|
|
|
if (s->avrctx_out)
|
|
|
|
avresample_close(s->avrctx_out);
|
2013-10-19 11:18:25 +00:00
|
|
|
avresample_free(&s->avrctx_out);
|
2013-03-09 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
static void reorder_planes(struct mp_audio *mpa, int *reorder,
|
|
|
|
struct mp_chmap *newmap)
|
2013-11-10 22:17:04 +00:00
|
|
|
{
|
|
|
|
struct mp_audio prev = *mpa;
|
2015-05-04 21:59:20 +00:00
|
|
|
|
|
|
|
for (int n = 0; n < newmap->num; n++) {
|
|
|
|
int src = reorder[n];
|
|
|
|
if (src < 0) {
|
|
|
|
src = 0; // Use the first plane for padding channels.
|
|
|
|
mpa->readonly = true;
|
|
|
|
}
|
|
|
|
assert(src < mpa->num_planes);
|
|
|
|
mpa->planes[n] = prev.planes[src];
|
2013-11-10 22:17:04 +00:00
|
|
|
}
|
2015-05-04 21:59:20 +00:00
|
|
|
|
|
|
|
mp_audio_set_channels(mpa, newmap);
|
2013-11-10 22:17:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:31 +00:00
|
|
|
static int filter(struct af_instance *af, struct mp_audio *in)
|
2013-03-09 08:30:26 +00:00
|
|
|
{
|
2013-07-22 12:47:12 +00:00
|
|
|
struct af_resample *s = af->priv;
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2015-06-02 20:20:04 +00:00
|
|
|
int samples = get_out_samples(s, in ? in->samples : 0);
|
2015-05-04 21:59:20 +00:00
|
|
|
|
|
|
|
struct mp_audio out_format = s->avrctx_fmt;
|
|
|
|
struct mp_audio *out = mp_audio_pool_get(af->out_pool, &out_format, samples);
|
2015-01-14 21:15:31 +00:00
|
|
|
if (!out)
|
|
|
|
goto error;
|
|
|
|
if (in)
|
|
|
|
mp_audio_copy_attributes(out, in);
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2013-11-10 22:39:29 +00:00
|
|
|
af->delay = get_delay(s) / (double)s->ctx.in_rate;
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2013-11-10 22:17:04 +00:00
|
|
|
if (out->samples) {
|
2015-01-14 21:15:31 +00:00
|
|
|
out->samples = resample_frame(s->avrctx, out, in);
|
2013-11-10 22:17:04 +00:00
|
|
|
if (out->samples < 0)
|
2015-01-14 21:15:31 +00:00
|
|
|
goto error;
|
2013-11-10 20:40:51 +00:00
|
|
|
}
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
if (out->samples && !mp_audio_config_equals(out, af->data)) {
|
|
|
|
assert(AF_FORMAT_IS_PLANAR(out->format));
|
|
|
|
reorder_planes(out, s->reorder_out, &af->data->channels);
|
|
|
|
if (!mp_audio_config_equals(out, af->data)) {
|
|
|
|
struct mp_audio *new = mp_audio_pool_get(s->reorder_buffer, af->data,
|
2015-01-14 21:15:31 +00:00
|
|
|
out->samples);
|
|
|
|
if (!new)
|
|
|
|
goto error;
|
|
|
|
mp_audio_copy_attributes(new, out);
|
|
|
|
int out_samples = resample_frame(s->avrctx_out, new, out);
|
|
|
|
talloc_free(out);
|
|
|
|
out = new;
|
|
|
|
if (out_samples != new->samples)
|
|
|
|
goto error;
|
2013-11-10 22:17:04 +00:00
|
|
|
}
|
2013-04-05 22:56:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:15:31 +00:00
|
|
|
talloc_free(in);
|
|
|
|
if (out->samples) {
|
|
|
|
af_add_output_frame(af, out);
|
|
|
|
} else {
|
|
|
|
talloc_free(out);
|
|
|
|
}
|
2013-12-04 23:01:46 +00:00
|
|
|
return 0;
|
2015-01-14 21:15:31 +00:00
|
|
|
error:
|
|
|
|
talloc_free(in);
|
|
|
|
talloc_free(out);
|
|
|
|
return -1;
|
2013-03-09 08:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int af_open(struct af_instance *af)
|
|
|
|
{
|
2013-07-22 12:47:12 +00:00
|
|
|
struct af_resample *s = af->priv;
|
2013-03-09 08:30:26 +00:00
|
|
|
|
|
|
|
af->control = control;
|
|
|
|
af->uninit = uninit;
|
2015-01-14 21:15:31 +00:00
|
|
|
af->filter_frame = filter;
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2013-07-22 12:47:12 +00:00
|
|
|
if (s->opts.cutoff <= 0.0)
|
|
|
|
s->opts.cutoff = af_resample_default_cutoff(s->opts.filter_size);
|
2013-03-29 21:29:13 +00:00
|
|
|
|
2013-03-09 08:30:26 +00:00
|
|
|
s->avrctx = avresample_alloc_context();
|
2013-04-05 22:56:01 +00:00
|
|
|
s->avrctx_out = avresample_alloc_context();
|
2015-01-14 21:15:31 +00:00
|
|
|
s->reorder_buffer = mp_audio_pool_create(s);
|
2013-03-09 08:30:26 +00:00
|
|
|
|
2013-04-05 22:56:01 +00:00
|
|
|
if (s->avrctx && s->avrctx_out) {
|
2013-03-09 08:30:26 +00:00
|
|
|
return AF_OK;
|
|
|
|
} else {
|
2014-08-17 01:29:09 +00:00
|
|
|
MP_ERR(af, "Cannot initialize Libavresample Context. \n");
|
2013-03-09 08:30:26 +00:00
|
|
|
uninit(af);
|
|
|
|
return AF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 12:47:12 +00:00
|
|
|
#define OPT_BASE_STRUCT struct af_resample
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
const struct af_info af_info_lavrresample = {
|
2013-10-23 17:05:47 +00:00
|
|
|
.info = "Sample frequency conversion using libavresample",
|
|
|
|
.name = "lavrresample",
|
|
|
|
.open = af_open,
|
2013-03-23 12:05:32 +00:00
|
|
|
.test_conversion = test_conversion,
|
2013-07-22 12:47:12 +00:00
|
|
|
.priv_size = sizeof(struct af_resample),
|
|
|
|
.priv_defaults = &(const struct af_resample) {
|
|
|
|
.opts = {
|
|
|
|
.filter_size = 16,
|
|
|
|
.cutoff = 0.0,
|
|
|
|
.phase_shift = 10,
|
|
|
|
},
|
2015-03-02 18:09:44 +00:00
|
|
|
.playback_speed = 1.0,
|
2013-07-22 12:47:12 +00:00
|
|
|
.allow_detach = 1,
|
|
|
|
},
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_INTRANGE("filter-size", opts.filter_size, 0, 0, 32),
|
|
|
|
OPT_INTRANGE("phase-shift", opts.phase_shift, 0, 0, 30),
|
|
|
|
OPT_FLAG("linear", opts.linear, 0),
|
|
|
|
OPT_DOUBLE("cutoff", opts.cutoff, M_OPT_RANGE, .min = 0, .max = 1),
|
|
|
|
OPT_FLAG("detach", allow_detach, 0),
|
2014-08-02 01:12:09 +00:00
|
|
|
OPT_KEYVALUELIST("o", avopts, 0),
|
2013-07-22 12:47:12 +00:00
|
|
|
{0}
|
|
|
|
},
|
2013-03-09 08:30:26 +00:00
|
|
|
};
|