static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = (struct af_resample *) af->priv; struct mp_audio *in = (struct mp_audio *) arg; struct mp_audio *out = (struct mp_audio *) af->data; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); if (af_to_avformat(out->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(out, in->format); af->mul = out->rate / (double)in->rate; int r = ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; if (r == AF_OK && needs_lavrctx_reconfigure(s, in, out)) r = configure_lavrr(af, in, out); return r; } case AF_CONTROL_SET_FORMAT: { if (af_to_avformat(*(int*)arg) == AV_SAMPLE_FMT_NONE) return AF_FALSE; mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } case AF_CONTROL_SET_CHANNELS: { mp_audio_set_channels(af->data, (struct mp_chmap *)arg); return AF_OK; } case AF_CONTROL_SET_RESAMPLE_RATE: out->rate = *(int *)arg; return AF_OK; case AF_CONTROL_RESET: drop_all_output(s); return AF_OK; } return AF_UNKNOWN; }
static int control(struct af_instance *af, int cmd, void *arg) { switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; struct mp_audio *out = af->data; if (!test_conversion(in->format, out->format)) return AF_DETACH; if ((in->format & AF_FORMAT_BITS_MASK) == AF_FORMAT_24BIT) { mp_audio_set_format(out, af_fmt_change_bits(in->format, 32)); } else if ((in->format & AF_FORMAT_BITS_MASK) == AF_FORMAT_32BIT) { mp_audio_set_format(out, af_fmt_change_bits(in->format, 24)); } else { abort(); } out->rate = in->rate; mp_audio_set_channels(out, &in->channels); assert(test_conversion(in->format, out->format)); return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE; } case AF_CONTROL_SET_FORMAT: { mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } } return AF_UNKNOWN; }
// fmt carries the input format. Change it to the best next-possible format // the encoder likely accepts. static void select_encode_format(AVCodecContext *c, struct mp_audio *fmt) { int formats[AF_FORMAT_COUNT]; af_get_best_sample_formats(fmt->format, formats); for (int n = 0; formats[n]; n++) { const enum AVSampleFormat *lf = c->codec->sample_fmts; for (int i = 0; lf && lf[i] != AV_SAMPLE_FMT_NONE; i++) { int mpfmt = af_from_avformat(lf[i]); if (mpfmt && mpfmt == formats[n]) { mp_audio_set_format(fmt, mpfmt); goto done_fmt; } } } done_fmt: ; int rate = af_select_best_samplerate(fmt->rate, c->codec->supported_samplerates); if (rate > 0) fmt->rate = rate; struct mp_chmap_sel sel = {0}; const uint64_t *lch = c->codec->channel_layouts; for (int n = 0; lch && lch[n]; n++) { struct mp_chmap chmap = {0}; mp_chmap_from_lavc(&chmap, lch[n]); mp_chmap_sel_add_map(&sel, &chmap); } struct mp_chmap res = fmt->channels; mp_chmap_sel_adjust(&sel, &res); if (!mp_chmap_is_empty(&res)) mp_audio_set_channels(fmt, &res); }
static void set_channels(struct mp_audio *mpa, int num) { struct mp_chmap map; // "unknown" channel layouts make it easier to pass through audio data, // without triggering remixing. mp_chmap_set_unknown(&map, num); mp_audio_set_channels(mpa, &map); }
static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = af->priv; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio *out = af->data; struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach && s->playback_speed == 1.0) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); if (check_output_conversion(out->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(out, in->format); int r = ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; if (r == AF_OK) r = configure_lavrr(af, in, out, true); return r; } case AF_CONTROL_SET_PLAYBACK_SPEED_RESAMPLE: { s->playback_speed = *(double *)arg; return AF_OK; } case AF_CONTROL_RESET: if (s->avrctx) { #if HAVE_LIBSWRESAMPLE swr_close(s->avrctx); if (swr_init(s->avrctx) < 0) { close_lavrr(af); return AF_ERROR; } #else while (avresample_read(s->avrctx, NULL, 1000) > 0) {} #endif } return AF_OK; } return AF_UNKNOWN; }
static int control(struct af_instance *af, int cmd, void *arg) { struct priv *p = af->priv; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; struct mp_audio *out = af->data; if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); // Removing this requires fixing AVFrame.data vs. AVFrame.extended_data if (in->channels.num > AV_NUM_DATA_POINTERS) return AF_ERROR; if (!mp_chmap_is_lavc(&in->channels)) mp_chmap_reorder_to_lavc(&in->channels); // will always work if (!recreate_graph(af, in)) return AF_ERROR; AVFilterLink *l_out = p->out->inputs[0]; out->rate = l_out->sample_rate; mp_audio_set_format(out, af_from_avformat(l_out->format)); struct mp_chmap out_cm; mp_chmap_from_lavc(&out_cm, l_out->channel_layout); mp_audio_set_channels(out, &out_cm); if (!mp_audio_config_valid(out) || out->channels.num > AV_NUM_DATA_POINTERS) return AF_ERROR; p->timebase_out = l_out->time_base; return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE; } case AF_CONTROL_GET_METADATA: if (p->metadata) { *(struct mp_tags *)arg = *p->metadata; return CONTROL_OK; } return CONTROL_NA; case AF_CONTROL_RESET: reset(af); return AF_OK; } return AF_UNKNOWN; }
static void force_in_params(struct af_instance *af, struct mp_audio *in) { struct priv *priv = af->priv; if (priv->in_format != AF_FORMAT_UNKNOWN) mp_audio_set_format(in, priv->in_format); if (priv->in_channels.num) mp_audio_set_channels(in, &priv->in_channels); if (priv->in_srate) in->rate = priv->in_srate; }
static void force_out_params(struct af_instance *af, struct mp_audio *out) { struct priv *priv = af->priv; if (priv->out_format != AF_FORMAT_UNKNOWN) mp_audio_set_format(out, priv->out_format); if (priv->out_channels.num) mp_audio_set_channels(out, &priv->out_channels); if (priv->out_srate) out->rate = priv->out_srate; }
int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate, int *out_samplerate, struct mp_chmap *out_channels, int *out_format) { if (!sh_audio->afilter) sh_audio->afilter = af_new(sh_audio->opts); struct af_stream *afs = sh_audio->afilter; // input format: same as codec's output format: afs->input.rate = in_samplerate; mp_audio_set_channels(&afs->input, &sh_audio->channels); mp_audio_set_format(&afs->input, sh_audio->sample_format); // output format: same as ao driver's input format (if missing, fallback to input) afs->output.rate = *out_samplerate; mp_audio_set_channels(&afs->output, out_channels); mp_audio_set_format(&afs->output, *out_format); char *s_from = mp_audio_config_to_str(&afs->input); char *s_to = mp_audio_config_to_str(&afs->output); mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %s -> %s...\n", s_from, s_to); talloc_free(s_from); talloc_free(s_to); // let's autoprobe it! if (af_init(afs) != 0) { af_destroy(afs); sh_audio->afilter = NULL; return 0; // failed :( } *out_samplerate = afs->output.rate; *out_channels = afs->output.channels; *out_format = afs->output.format; return 1; }
static int control(struct af_instance *af, int cmd, void *arg) { struct priv *p = af->priv; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; struct mp_audio *out = af->data; if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); if (!mp_chmap_is_lavc(&in->channels)) mp_chmap_reorder_to_lavc(&in->channels); // will always work if (!recreate_graph(af, in)) return AF_ERROR; AVFilterLink *l_out = p->out->inputs[0]; out->rate = l_out->sample_rate; mp_audio_set_format(out, af_from_avformat(l_out->format)); struct mp_chmap out_cm; mp_chmap_from_lavc(&out_cm, l_out->channel_layout); if (!out_cm.num || out_cm.num != l_out->channels) mp_chmap_from_channels(&out_cm, l_out->channels); mp_audio_set_channels(out, &out_cm); if (!mp_audio_config_valid(out)) return AF_ERROR; p->timebase_out = l_out->time_base; // Blatantly incorrect; we don't know what the filters do. af->mul = out->rate / (double)in->rate; return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE; } } return AF_UNKNOWN; }
int audio_init_filters(struct dec_audio *d_audio, int in_samplerate, int *out_samplerate, struct mp_chmap *out_channels, int *out_format) { if (!d_audio->afilter) d_audio->afilter = af_new(d_audio->global); struct af_stream *afs = d_audio->afilter; // input format: same as codec's output format: mp_audio_buffer_get_format(d_audio->decode_buffer, &afs->input); // Sample rate can be different when adjusting playback speed afs->input.rate = in_samplerate; // output format: same as ao driver's input format (if missing, fallback to input) afs->output.rate = *out_samplerate; mp_audio_set_channels(&afs->output, out_channels); mp_audio_set_format(&afs->output, *out_format); afs->metadata = d_audio->metadata; afs->replaygain_data = d_audio->replaygain_data; char *s_from = mp_audio_config_to_str(&afs->input); char *s_to = mp_audio_config_to_str(&afs->output); MP_VERBOSE(d_audio, "Building audio filter chain for %s -> %s...\n", s_from, s_to); talloc_free(s_from); talloc_free(s_to); // let's autoprobe it! if (af_init(afs) != 0) { af_destroy(afs); d_audio->afilter = NULL; return 0; // failed :( } *out_samplerate = afs->output.rate; *out_channels = afs->output.channels; *out_format = afs->output.format; return 1; }
// Initialization and runtime control static int control(struct af_instance* af, int cmd, void* arg) { af_channels_t* s = af->priv; switch(cmd){ case AF_CONTROL_REINIT: ; struct mp_chmap chmap; mp_chmap_set_unknown(&chmap, s->nch); mp_audio_set_channels(af->data, &chmap); // Set default channel assignment if(!s->router){ int i; // Make sure this filter isn't redundant if(af->data->nch == ((struct mp_audio*)arg)->nch) return AF_DETACH; // If mono: fake stereo if(((struct mp_audio*)arg)->nch == 1){ s->nr = MPMIN(af->data->nch,2); for(i=0;i<s->nr;i++){ s->route[i][FR] = 0; s->route[i][TO] = i; } } else{ s->nr = MPMIN(af->data->nch, ((struct mp_audio*)arg)->nch); for(i=0;i<s->nr;i++){ s->route[i][FR] = i; s->route[i][TO] = i; } } } af->data->rate = ((struct mp_audio*)arg)->rate; mp_audio_force_interleaved_format((struct mp_audio*)arg); mp_audio_set_format(af->data, ((struct mp_audio*)arg)->format); return check_routes(af,((struct mp_audio*)arg)->nch,af->data->nch); } return AF_UNKNOWN; }
// Filter data through filter static int filter(struct af_instance* af, struct mp_audio* data, int flags) { struct mp_audio* c = data; // Current working data struct mp_audio* l = af->data; // Local data af_channels_t* s = af->priv; int i; mp_audio_realloc_min(af->data, data->samples); // Reset unused channels memset(l->planes[0],0,mp_audio_psize(c) / c->nch * l->nch); if(AF_OK == check_routes(af,c->nch,l->nch)) for(i=0;i<s->nr;i++) copy(af, c->planes[0],l->planes[0],c->nch,s->route[i][FR], l->nch,s->route[i][TO],mp_audio_psize(c),c->bps); // Set output data c->planes[0] = l->planes[0]; mp_audio_set_channels(c, &l->channels); return 0; }
static int control(struct af_instance *af, int cmd, void *arg) { switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; struct mp_audio *out = af->data; if (!test_conversion(in->format, out->format)) return AF_DETACH; out->rate = in->rate; mp_audio_set_channels(out, &in->channels); return mp_audio_config_equals(in, &orig_in) ? AF_OK : AF_FALSE; } case AF_CONTROL_SET_FORMAT: { mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } } return AF_UNKNOWN; }
static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = af->priv; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio *out = af->data; struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach && s->playback_speed == 1.0) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); if (check_output_conversion(out->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(out, in->format); int r = ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; if (r == AF_OK && needs_lavrctx_reconfigure(s, in, out)) r = configure_lavrr(af, in, out); return r; } case AF_CONTROL_SET_FORMAT: { int format = *(int *)arg; if (format && check_output_conversion(format) == AV_SAMPLE_FMT_NONE) return AF_FALSE; mp_audio_set_format(af->data, format); return AF_OK; } case AF_CONTROL_SET_CHANNELS: { mp_audio_set_channels(af->data, (struct mp_chmap *)arg); return AF_OK; } case AF_CONTROL_SET_RESAMPLE_RATE: af->data->rate = *(int *)arg; return AF_OK; 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 && af->fmt_out.format) { // Before reconfiguring, drain the audio that is still buffered // in the resampler. af->filter_frame(af, NULL); // Reinitialize resampler. configure_lavrr(af, &af->fmt_in, &af->fmt_out); } return AF_OK; } case AF_CONTROL_RESET: if (s->avrctx) drop_all_output(s); return AF_OK; } return AF_UNKNOWN; }
static int configure_lavrr(struct af_instance *af, struct mp_audio *in, struct mp_audio *out, bool verbose) { struct af_resample *s = af->priv; close_lavrr(af); s->avrctx = avresample_alloc_context(); s->avrctx_out = avresample_alloc_context(); if (!s->avrctx || !s->avrctx_out) goto error; enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); enum AVSampleFormat out_samplefmt = check_output_conversion(out->format); enum AVSampleFormat out_samplefmtp = av_get_planar_sample_fmt(out_samplefmt); if (in_samplefmt == AV_SAMPLE_FMT_NONE || out_samplefmt == AV_SAMPLE_FMT_NONE || out_samplefmtp == AV_SAMPLE_FMT_NONE) goto error; s->out_rate = out->rate; s->in_rate_af = in->rate; s->in_rate = rate_from_speed(in->rate, s->playback_speed); s->out_format = out->format; s->in_format = in->format; s->out_channels= out->channels; s->in_channels = in->channels; av_opt_set_int(s->avrctx, "filter_size", s->opts.filter_size, 0); av_opt_set_int(s->avrctx, "phase_shift", s->opts.phase_shift, 0); av_opt_set_int(s->avrctx, "linear_interp", s->opts.linear, 0); av_opt_set_double(s->avrctx, "cutoff", s->opts.cutoff, 0); int normalize = s->opts.normalize; if (normalize < 0) normalize = af->opts->audio_normalize; #if HAVE_LIBSWRESAMPLE av_opt_set_double(s->avrctx, "rematrix_maxval", normalize ? 1 : 1000, 0); #else av_opt_set_int(s->avrctx, "normalize_mix_level", !!normalize, 0); #endif if (mp_set_avopts(af->log, s->avrctx, s->avopts) < 0) goto 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, out_lavc; mp_chmap_from_lavc(&in_lavc, in_ch_layout); mp_chmap_from_lavc(&out_lavc, out_ch_layout); if (verbose && !mp_chmap_equals(&in_lavc, &out_lavc)) { MP_VERBOSE(af, "Remix: %s -> %s\n", mp_chmap_to_str(&in_lavc), mp_chmap_to_str(&out_lavc)); } 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"); goto error; } mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc); transpose_order(s->reorder_in, map_in.num); 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) goto 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)); s->pre_out_fmt = *out; mp_audio_set_format(&s->pre_out_fmt, af_from_avformat(out_samplefmt)); // If there are NA channels, the final output will have more channels than // the avrctx output. Also, avrctx will output planar (out_samplefmtp was // not overwritten). Allocate the output frame with more channels, so the // NA channels can be trivially added. s->pool_fmt = s->avrctx_fmt; if (map_out.num > out_lavc.num) mp_audio_set_channels(&s->pool_fmt, &map_out); out_ch_layout = fudge_layout_conversion(af, in_ch_layout, out_ch_layout); // 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->in_rate, 0); av_opt_set_int(s->avrctx, "out_sample_rate", s->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 for deplanarization. struct mp_chmap fake_chmap; mp_chmap_set_unknown(&fake_chmap, map_out.num); uint64_t fake_out_ch_layout = mp_chmap_to_lavc_unchecked(&fake_chmap); if (!fake_out_ch_layout) goto error; 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); av_opt_set_int(s->avrctx_out, "out_sample_fmt", out_samplefmt, 0); av_opt_set_int(s->avrctx_out, "in_sample_rate", s->out_rate, 0); av_opt_set_int(s->avrctx_out, "out_sample_rate", s->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) { MP_ERR(af, "Cannot open Libavresample Context. \n"); goto error; } return AF_OK; error: close_lavrr(af); return AF_ERROR; }
int AudioController::reinitialize(mp_audio *in) { if (!in) return AF_ERROR; auto makeFormat = [] (const mp_audio *audio) { AudioFormat format; format.m_samplerate = audio->rate/1000.0; // kHz format.m_bitrate = audio->rate*audio->nch*audio->bps*8; format.m_bits = audio->bps*8; format.m_channels = ChannelLayoutInfo::description(ChannelLayoutMap::toLayout(audio->channels)); format.m_type = af_fmt_to_str(audio->format); return format; }; d->input = makeFormat(in); auto out = d->af->data; out->rate = in->rate; bool ret = true; if (!isSupported(in->format)) { ret = false; mp_audio_set_format(in, af_fmt_is_planar(in->format) ? AF_FORMAT_FLOATP : AF_FORMAT_FLOAT); } if (d->fmt_conv) { mp_audio_set_format(out, d->fmt_conv); d->fmt_conv = AF_FORMAT_UNKNOWN; } else mp_audio_set_format(out, in->format); d->chmap = in->channels; if (!mp_chmap_from_str(&d->chmap, bstr0(ChannelLayoutInfo::data(d->layout).constData()))) _Error("Cannot find matched channel layout for '%%'", ChannelLayoutInfo::description(d->layout)); mp_audio_set_channels(out, &d->chmap); if (d->outrate != 0) out->rate = d->outrate; if (!ret) return false; d->af->mul = (double)out->channels.num/in->channels.num; if (d->tempoScalerActivated) d->af->mul /= d->scale; if ((d->resample = out->rate != in->rate)) { d->af->mul *= (double)out->rate/in->rate; const auto nch = in->channels.num;/*mp_chmap_to_lavc_unchecked(&in->channels);*/ const auto fmt = af_to_avformat(in->format); if (!d->swr) d->swr = swr_alloc(); av_opt_set_int(d->swr, "in_channel_count", nch, 0); av_opt_set_int(d->swr, "out_channel_count", nch, 0); av_opt_set_int(d->swr, "in_sample_rate", in->rate, 0); av_opt_set_int(d->swr, "out_sample_rate", out->rate, 0); av_opt_set_sample_fmt(d->swr, "in_sample_fmt", fmt, 0); av_opt_set_sample_fmt(d->swr, "out_sample_fmt", fmt, 0); swr_init(d->swr); if (!d->resampled) d->resampled = talloc_zero(nullptr, mp_audio); *d->resampled = *in; d->resampled->rate = out->rate; in = d->resampled; } d->output = makeFormat(out); const AudioDataFormat fmt_in(*in), fmt_out(*out); check(d->mixer, d->clip, fmt_in, fmt_out); d->mixer->setOutput(out); d->mixer->setChannelLayoutMap(d->map); d->dirty = 0xffffffff; return true; }
static int filter_n_bytes(sh_audio_t *sh, struct bstr *outbuf, int len) { assert(len - 1 + sh->audio_out_minsize <= sh->a_buffer_size); int error = 0; // Decode more bytes if needed int old_samplerate = sh->samplerate; struct mp_chmap old_channels = sh->channels; int old_sample_format = sh->sample_format; while (sh->a_buffer_len < len) { unsigned char *buf = sh->a_buffer + sh->a_buffer_len; int minlen = len - sh->a_buffer_len; int maxlen = sh->a_buffer_size - sh->a_buffer_len; int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen); int format_change = sh->samplerate != old_samplerate || !mp_chmap_equals(&sh->channels, &old_channels) || sh->sample_format != old_sample_format; if (ret <= 0 || format_change) { error = format_change ? -2 : -1; // samples from format-changing call get discarded too len = sh->a_buffer_len; break; } sh->a_buffer_len += ret; } // Filter struct mp_audio filter_input = { .audio = sh->a_buffer, .len = len, .rate = sh->samplerate, }; mp_audio_set_format(&filter_input, sh->sample_format); mp_audio_set_channels(&filter_input, &sh->channels); struct mp_audio *filter_output = af_play(sh->afilter, &filter_input); if (!filter_output) return -1; set_min_out_buffer_size(outbuf, outbuf->len + filter_output->len); memcpy(outbuf->start + outbuf->len, filter_output->audio, filter_output->len); outbuf->len += filter_output->len; // remove processed data from decoder buffer: sh->a_buffer_len -= len; memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len); return error; } /* Try to get at least minlen decoded+filtered bytes in outbuf * (total length including possible existing data). * Return 0 on success, -1 on error/EOF (not distinguished). * In the former case outbuf->len is always >= minlen on return. * In case of EOF/error it might or might not be. * Outbuf.start must be talloc-allocated, and will be reallocated * if needed to fit all filter output. */ int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen) { // Indicates that a filter seems to be buffering large amounts of data int huge_filter_buffer = 0; // Decoded audio must be cut at boundaries of this many bytes int unitsize = sh_audio->channels.num * sh_audio->samplesize * 16; /* Filter output size will be about filter_multiplier times input size. * If some filter buffers audio in big blocks this might only hold * as average over time. */ double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter); /* If the decoder set audio_out_minsize then it can do the equivalent of * "while (output_len < target_len) output_len += audio_out_minsize;", * so we must guarantee there is at least audio_out_minsize-1 bytes * more space in the output buffer than the minimum length we try to * decode. */ int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize; if (!unitsize) return -1; max_decode_len -= max_decode_len % unitsize; while (minlen >= 0 && outbuf->len < minlen) { // + some extra for possible filter buffering int declen = (minlen - outbuf->len) / filter_multiplier + (unitsize << 5); if (huge_filter_buffer) /* Some filter must be doing significant buffering if the estimated * input length didn't produce enough output from filters. * Feed the filters 2k bytes at a time until we have enough output. * Very small amounts could make filtering inefficient while large * amounts can make MPlayer demux the file unnecessarily far ahead * to get audio data and buffer video frames in memory while doing * so. However the performance impact of either is probably not too * significant as long as the value is not completely insane. */ declen = 2000; declen -= declen % unitsize; if (declen > max_decode_len) declen = max_decode_len; else /* if this iteration does not fill buffer, we must have lots * of buffering in filters */ huge_filter_buffer = 1; int res = filter_n_bytes(sh_audio, outbuf, declen); if (res < 0) return res; } return 0; }
static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = (struct af_resample *) af->priv; struct mp_audio *in = (struct mp_audio *) arg; struct mp_audio *out = (struct mp_audio *) af->data; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); enum AVSampleFormat in_samplefmt = af_to_avformat(in->format); if (in_samplefmt == AV_SAMPLE_FMT_NONE) { mp_audio_set_format(in, AF_FORMAT_FLOAT_NE); in_samplefmt = af_to_avformat(in->format); } enum AVSampleFormat out_samplefmt = af_to_avformat(out->format); if (out_samplefmt == AV_SAMPLE_FMT_NONE) { mp_audio_set_format(out, in->format); out_samplefmt = in_samplefmt; } af->mul = (double) (out->rate * out->nch) / (in->rate * in->nch); af->delay = out->nch * s->opts.filter_size / FFMIN(af->mul, 1); if (needs_lavrctx_reconfigure(s, in, out)) { avresample_close(s->avrctx); avresample_close(s->avrctx_out); s->ctx.out_rate = out->rate; s->ctx.in_rate = in->rate; 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; ctx_opt_set_int("filter_size", s->ctx.filter_size); ctx_opt_set_int("phase_shift", s->ctx.phase_shift); ctx_opt_set_int("linear_interp", s->ctx.linear); ctx_opt_set_dbl("cutoff", s->ctx.cutoff); if (parse_avopts(s->avrctx, s->avopts) < 0) { mp_msg(MSGT_VFILTER, MSGL_FATAL, "af_lavrresample: could not set opts: '%s'\n", s->avopts); 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); ctx_opt_set_int("in_channel_layout", in_ch_layout); ctx_opt_set_int("out_channel_layout", out_ch_layout); ctx_opt_set_int("in_sample_rate", s->ctx.in_rate); ctx_opt_set_int("out_sample_rate", s->ctx.out_rate); ctx_opt_set_int("in_sample_fmt", in_samplefmt); ctx_opt_set_int("out_sample_fmt", out_samplefmt); struct mp_chmap in_lavc; mp_chmap_from_lavc(&in_lavc, in_ch_layout); mp_chmap_get_reorder(s->reorder_in, &map_in, &in_lavc); struct mp_chmap out_lavc; mp_chmap_from_lavc(&out_lavc, out_ch_layout); mp_chmap_get_reorder(s->reorder_out, &out_lavc, &map_out); // Same configuration; we just reorder. av_opt_set_int(s->avrctx_out, "in_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "out_channel_layout", out_ch_layout, 0); av_opt_set_int(s->avrctx_out, "in_sample_fmt", out_samplefmt, 0); 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); #if USE_SET_CHANNEL_MAPPING // 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); avresample_set_channel_mapping(s->avrctx_out, s->reorder_out); #endif if (avresample_open(s->avrctx) < 0 || avresample_open(s->avrctx_out) < 0) { mp_msg(MSGT_AFILTER, MSGL_ERR, "[lavrresample] Cannot open " "Libavresample Context. \n"); return AF_ERROR; } } return ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; } case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET: { if (af_to_avformat(*(int*)arg) == AV_SAMPLE_FMT_NONE) return AF_FALSE; mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } case AF_CONTROL_CHANNELS | AF_CONTROL_SET: { mp_audio_set_channels(af->data, (struct mp_chmap *)arg); return AF_OK; } case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET: out->rate = *(int *)arg; return AF_OK; } return AF_UNKNOWN; }
// Use old MPlayer/ALSA channel layout. void mp_audio_set_channels_old(struct mp_audio *mpa, int num_channels) { struct mp_chmap map; mp_chmap_from_channels_alsa(&map, num_channels); mp_audio_set_channels(mpa, &map); }
void mp_audio_copy_config(struct mp_audio *dst, const struct mp_audio *src) { mp_audio_set_format(dst, src->format); mp_audio_set_channels(dst, &src->channels); dst->rate = src->rate; }
// Initialization and runtime control static int control(struct af_instance* af, int cmd, void* arg) { switch(cmd){ case AF_CONTROL_REINIT:{ char buf1[256]; char buf2[256]; struct mp_audio *data = arg; int supported_ac3 = 0; // Make sure this filter isn't redundant if(af->data->format == data->format) return AF_DETACH; // A bit complex because we can convert AC3 // to generic iec61937 but not the other way // round. if (AF_FORMAT_IS_AC3(af->data->format)) supported_ac3 = AF_FORMAT_IS_AC3(data->format); else if (AF_FORMAT_IS_IEC61937(af->data->format)) supported_ac3 = AF_FORMAT_IS_IEC61937(data->format); // Allow trivial AC3-endianness conversion if (!supported_ac3) // Check for errors in configuration if((AF_OK != check_bps(data->bps)) || (AF_OK != check_format(data->format)) || (AF_OK != check_bps(af->data->bps)) || (AF_OK != check_format(af->data->format))) return AF_ERROR; af_fmt2str(data->format,buf1,256); af_fmt2str(af->data->format,buf2,256); mp_msg(MSGT_AFILTER, MSGL_V, "[format] Changing sample format from %s to %s\n", buf1, buf2); af->data->rate = data->rate; mp_audio_set_channels(af->data, &data->channels); af->mul = (double)af->data->bps / data->bps; af->play = play; // set default // look whether only endianness differences are there if ((af->data->format & ~AF_FORMAT_END_MASK) == (data->format & ~AF_FORMAT_END_MASK)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated endianness conversion only\n"); af->play = play_swapendian; } if ((data->format == AF_FORMAT_FLOAT_NE) && (af->data->format == AF_FORMAT_S16_NE)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", buf1, buf2); af->play = play_float_s16; } if ((data->format == AF_FORMAT_S16_NE) && (af->data->format == AF_FORMAT_FLOAT_NE)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", buf1, buf2); af->play = play_s16_float; } return AF_OK; } case AF_CONTROL_COMMAND_LINE:{ int format = af_str2fmt_short(bstr0(arg)); if (!format) { mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg); return AF_ERROR; } if(AF_OK != af->control(af, AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) return AF_ERROR; return AF_OK; } case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ // Check for errors in configuration if(!AF_FORMAT_IS_AC3(*(int*)arg) && AF_OK != check_format(*(int*)arg)) return AF_ERROR; mp_audio_set_format(af->data, *(int*)arg); return AF_OK; } } return AF_UNKNOWN; }
static int control(struct af_instance *af, int cmd, void *arg) { struct af_resample *s = af->priv; switch (cmd) { case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio *out = af->data; struct mp_audio orig_in = *in; if (((out->rate == in->rate) || (out->rate == 0)) && (out->format == in->format) && (mp_chmap_equals(&out->channels, &in->channels) || out->nch == 0) && s->allow_detach && s->playback_speed == 1.0) return AF_DETACH; if (out->rate == 0) out->rate = in->rate; if (mp_chmap_is_empty(&out->channels)) mp_audio_set_channels(out, &in->channels); if (af_to_avformat(in->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(in, AF_FORMAT_FLOAT); if (check_output_conversion(out->format) == AV_SAMPLE_FMT_NONE) mp_audio_set_format(out, in->format); int r = ((in->format == orig_in.format) && mp_chmap_equals(&in->channels, &orig_in.channels)) ? AF_OK : AF_FALSE; if (r == AF_OK) r = configure_lavrr(af, in, out, true); return r; } case AF_CONTROL_SET_FORMAT: { int format = *(int *)arg; if (format && check_output_conversion(format) == AV_SAMPLE_FMT_NONE) return AF_FALSE; mp_audio_set_format(af->data, format); return AF_OK; } case AF_CONTROL_SET_CHANNELS: { mp_audio_set_channels(af->data, (struct mp_chmap *)arg); return AF_OK; } case AF_CONTROL_SET_RESAMPLE_RATE: af->data->rate = *(int *)arg; return AF_OK; case AF_CONTROL_SET_PLAYBACK_SPEED_RESAMPLE: { s->playback_speed = *(double *)arg; return AF_OK; } case AF_CONTROL_RESET: if (s->avrctx) drop_all_output(s); return AF_OK; } return AF_UNKNOWN; }