1526 lines · 54.2 KB · C
RawDownload
/* * Copyright (c) 2017 Paul B Mahol * * This file is part of FFmpeg. * * FFmpeg 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. * * FFmpeg 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/avassert.h"#include "libavutil/channel_layout.h"#include "libavutil/mem.h"#include "libavutil/opt.h"#include "libavutil/tx.h"#include "avfilter.h"#include "audio.h"#include "filters.h"#include "formats.h"#include "window_func.h" enum SurroundChannel {    SC_FL, SC_FR, SC_FC, SC_LF, SC_BL, SC_BR, SC_BC, SC_SL, SC_SR,    SC_NB,}; static const int ch_map[SC_NB] = {    [SC_FL] = AV_CHAN_FRONT_LEFT,    [SC_FR] = AV_CHAN_FRONT_RIGHT,    [SC_FC] = AV_CHAN_FRONT_CENTER,    [SC_LF] = AV_CHAN_LOW_FREQUENCY,    [SC_BL] = AV_CHAN_BACK_LEFT,    [SC_BR] = AV_CHAN_BACK_RIGHT,    [SC_BC] = AV_CHAN_BACK_CENTER,    [SC_SL] = AV_CHAN_SIDE_LEFT,    [SC_SR] = AV_CHAN_SIDE_RIGHT,}; static const int sc_map[16] = {    [AV_CHAN_FRONT_LEFT   ] = SC_FL,    [AV_CHAN_FRONT_RIGHT  ] = SC_FR,    [AV_CHAN_FRONT_CENTER ] = SC_FC,    [AV_CHAN_LOW_FREQUENCY] = SC_LF,    [AV_CHAN_BACK_LEFT    ] = SC_BL,    [AV_CHAN_BACK_RIGHT   ] = SC_BR,    [AV_CHAN_BACK_CENTER  ] = SC_BC,    [AV_CHAN_SIDE_LEFT    ] = SC_SL,    [AV_CHAN_SIDE_RIGHT   ] = SC_SR,}; typedef struct AudioSurroundContext {    const AVClass *class;     AVChannelLayout out_ch_layout;    AVChannelLayout in_ch_layout;     float level_in;    float level_out;    float f_i[SC_NB];    float f_o[SC_NB];    int   lfe_mode;    float smooth;    float angle;    float focus;    int   win_size;    int   win_func;    float win_gain;    float overlap;     float all_x;    float all_y;     float f_x[SC_NB];    float f_y[SC_NB];     float *input_levels;    float *output_levels;    int output_lfe;    int create_lfe;    int lowcutf;    int highcutf;     float lowcut;    float highcut;     int nb_in_channels;    int nb_out_channels;     AVFrame *factors;    AVFrame *sfactors;    AVFrame *input_in;    AVFrame *input;    AVFrame *output;    AVFrame *output_mag;    AVFrame *output_ph;    AVFrame *output_out;    AVFrame *overlap_buffer;    AVFrame *window;     float *x_pos;    float *y_pos;    float *l_phase;    float *r_phase;    float *c_phase;    float *c_mag;    float *lfe_mag;    float *lfe_phase;    float *mag_total;     int rdft_size;    int hop_size;    AVTXContext **rdft, **irdft;    av_tx_fn tx_fn, itx_fn;    float *window_func_lut;     void (*filter)(AVFilterContext *ctx);    void (*upmix)(AVFilterContext *ctx, int ch);    void (*upmix_5_0)(AVFilterContext *ctx,                      float c_re, float c_im,                      float mag_totall, float mag_totalr,                      float fl_phase, float fr_phase,                      float bl_phase, float br_phase,                      float sl_phase, float sr_phase,                      float xl, float yl,                      float xr, float yr,                      int n);    void (*upmix_5_1)(AVFilterContext *ctx,                      float c_re, float c_im,                      float lfe_re, float lfe_im,                      float mag_totall, float mag_totalr,                      float fl_phase, float fr_phase,                      float bl_phase, float br_phase,                      float sl_phase, float sr_phase,                      float xl, float yl,                      float xr, float yr,                      int n);} AudioSurroundContext; static int query_formats(const AVFilterContext *ctx,                         AVFilterFormatsConfig **cfg_in,                         AVFilterFormatsConfig **cfg_out){    static const enum AVSampleFormat formats[] = {        AV_SAMPLE_FMT_FLTP,        AV_SAMPLE_FMT_NONE,    };     const AudioSurroundContext *s = ctx->priv;    AVFilterChannelLayouts *layouts = NULL;    int ret;     ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out, formats);    if (ret)        return ret;     layouts = NULL;    ret = ff_add_channel_layout(&layouts, &s->out_ch_layout);    if (ret)        return ret;     ret = ff_channel_layouts_ref(layouts, &cfg_out[0]->channel_layouts);    if (ret)        return ret;     layouts = NULL;    ret = ff_add_channel_layout(&layouts, &s->in_ch_layout);    if (ret)        return ret;     ret = ff_channel_layouts_ref(layouts, &cfg_in[0]->channel_layouts);    if (ret)        return ret;     return 0;} static void set_input_levels(AVFilterContext *ctx){    AudioSurroundContext *s = ctx->priv;     for (int ch = 0;  ch < s->nb_in_channels && s->level_in >= 0.f; ch++)        s->input_levels[ch] = s->level_in;    s->level_in = -1.f;     for (int n = 0; n < SC_NB; n++) {        const int ch = av_channel_layout_index_from_channel(&s->in_ch_layout, ch_map[n]);        if (ch >= 0)            s->input_levels[ch] = s->f_i[n];    }} static void set_output_levels(AVFilterContext *ctx){    AudioSurroundContext *s = ctx->priv;     for (int ch = 0;  ch < s->nb_out_channels && s->level_out >= 0.f; ch++)        s->output_levels[ch] = s->level_out;    s->level_out = -1.f;     for (int n = 0; n < SC_NB; n++) {        const int ch = av_channel_layout_index_from_channel(&s->out_ch_layout, ch_map[n]);        if (ch >= 0)            s->output_levels[ch] = s->f_o[n];    }} static int config_input(AVFilterLink *inlink){    AVFilterContext *ctx = inlink->dst;    AudioSurroundContext *s = ctx->priv;    int ret;     s->rdft = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->rdft));    if (!s->rdft)        return AVERROR(ENOMEM);    s->nb_in_channels = inlink->ch_layout.nb_channels;     for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {        float scale = 1.f;         ret = av_tx_init(&s->rdft[ch], &s->tx_fn, AV_TX_FLOAT_RDFT,                         0, s->win_size, &scale, 0);        if (ret < 0)            return ret;    }     s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));    if (!s->input_levels)        return AVERROR(ENOMEM);     set_input_levels(ctx);     s->window = ff_get_audio_buffer(inlink, s->win_size * 2);    if (!s->window)        return AVERROR(ENOMEM);     s->input_in = ff_get_audio_buffer(inlink, s->win_size * 2);    if (!s->input_in)        return AVERROR(ENOMEM);     s->input = ff_get_audio_buffer(inlink, s->win_size + 2);    if (!s->input)        return AVERROR(ENOMEM);     s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->win_size / 2);    s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->win_size / 2);     return 0;} static int config_output(AVFilterLink *outlink){    AVFilterContext *ctx = outlink->src;    AudioSurroundContext *s = ctx->priv;    int ret;     s->irdft = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->irdft));    if (!s->irdft)        return AVERROR(ENOMEM);    s->nb_out_channels = outlink->ch_layout.nb_channels;     for (int ch = 0; ch < outlink->ch_layout.nb_channels; ch++) {        float iscale = 1.f;         ret = av_tx_init(&s->irdft[ch], &s->itx_fn, AV_TX_FLOAT_RDFT,                         1, s->win_size, &iscale, 0);        if (ret < 0)            return ret;    }     s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));    if (!s->output_levels)        return AVERROR(ENOMEM);     set_output_levels(ctx);     s->factors = ff_get_audio_buffer(outlink, s->win_size + 2);    s->sfactors = ff_get_audio_buffer(outlink, s->win_size + 2);    s->output_ph = ff_get_audio_buffer(outlink, s->win_size + 2);    s->output_mag = ff_get_audio_buffer(outlink, s->win_size + 2);    s->output_out = ff_get_audio_buffer(outlink, s->win_size + 2);    s->output = ff_get_audio_buffer(outlink, s->win_size + 2);    s->overlap_buffer = ff_get_audio_buffer(outlink, s->win_size * 2);    if (!s->overlap_buffer || !s->output || !s->output_out || !s->output_mag ||        !s->output_ph || !s->factors || !s->sfactors)        return AVERROR(ENOMEM);     s->rdft_size = s->win_size / 2 + 1;     s->x_pos = av_calloc(s->rdft_size, sizeof(*s->x_pos));    s->y_pos = av_calloc(s->rdft_size, sizeof(*s->y_pos));    s->l_phase = av_calloc(s->rdft_size, sizeof(*s->l_phase));    s->r_phase = av_calloc(s->rdft_size, sizeof(*s->r_phase));    s->c_mag   = av_calloc(s->rdft_size, sizeof(*s->c_mag));    s->c_phase = av_calloc(s->rdft_size, sizeof(*s->c_phase));    s->mag_total = av_calloc(s->rdft_size, sizeof(*s->mag_total));    s->lfe_mag = av_calloc(s->rdft_size, sizeof(*s->lfe_mag));    s->lfe_phase = av_calloc(s->rdft_size, sizeof(*s->lfe_phase));    if (!s->x_pos || !s->y_pos || !s->l_phase || !s->r_phase || !s->lfe_phase ||        !s->c_phase || !s->mag_total || !s->lfe_mag || !s->c_mag)        return AVERROR(ENOMEM);     return 0;} static float sqrf(float x){    return x * x;} static float r_distance(float a){    return fminf(sqrtf(1.f + sqrf(tanf(a))), sqrtf(1.f + sqrf(1.f / tanf(a))));} #define MIN_MAG_SUM 0.00000001f static void angle_transform(float *x, float *y, float angle){    float reference, r, a;     if (angle == 90.f)        return;     reference = angle * M_PIf / 180.f;    r = hypotf(*x, *y);    a = atan2f(*x, *y);     r /= r_distance(a);     if (fabsf(a) <= M_PI_4f)        a *= reference / M_PI_2f;    else        a = M_PIf + (-2.f * M_PIf + reference) * (M_PIf - fabsf(a)) * FFDIFFSIGN(a, 0.f) / (3.f * M_PI_2f);     r *= r_distance(a);     *x = av_clipf(sinf(a) * r, -1.f, 1.f);    *y = av_clipf(cosf(a) * r, -1.f, 1.f);} static void focus_transform(float *x, float *y, float focus){    float a, r, ra;     if (focus == 0.f)        return;     a = atan2f(*x, *y);    ra = r_distance(a);    r = av_clipf(hypotf(*x, *y) / ra, 0.f, 1.f);    r = focus > 0.f ? 1.f - powf(1.f - r, 1.f + focus * 20.f) : powf(r, 1.f - focus * 20.f);    r *= ra;    *x = av_clipf(sinf(a) * r, -1.f, 1.f);    *y = av_clipf(cosf(a) * r, -1.f, 1.f);} static void stereo_position(float a, float p, float *x, float *y){    av_assert2(a >= -1.f && a <= 1.f);    av_assert2(p >= 0.f && p <= M_PIf);    *x = av_clipf(a+a*fmaxf(0.f, p*p-M_PI_2f), -1.f, 1.f);    *y = av_clipf(cosf(a*M_PI_2f+M_PIf)*cosf(M_PI_2f-p/M_PIf)*M_LN10f+1.f, -1.f, 1.f);} static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,                           float *lfe_mag, float c_mag, float *mag_total, int lfe_mode){    if (output_lfe && n < highcut) {        *lfe_mag    = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PIf*(lowcut-n)/(lowcut-highcut)));        *lfe_mag   *= c_mag;        if (lfe_mode)            *mag_total -= *lfe_mag;    } else {        *lfe_mag = 0.f;    }} #define TRANSFORM                         \        dst[2 * n    ] = mag * cosf(ph);  \        dst[2 * n + 1] = mag * sinf(ph); static void calculate_factors(AVFilterContext *ctx, int ch, int chan){    AudioSurroundContext *s = ctx->priv;    float *factor = (float *)s->factors->extended_data[ch];    const float f_x = s->f_x[sc_map[chan >= 0 ? chan : 0]];    const float f_y = s->f_y[sc_map[chan >= 0 ? chan : 0]];    const int rdft_size = s->rdft_size;    const float *x = s->x_pos;    const float *y = s->y_pos;     switch (chan) {    case AV_CHAN_FRONT_CENTER:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((y[n] + 1.f) * .5f, f_y);        break;    case AV_CHAN_FRONT_LEFT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf((y[n] + 1.f) * .5f, f_y);        break;    case AV_CHAN_FRONT_RIGHT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf((y[n] + 1.f) * .5f, f_y);        break;    case AV_CHAN_LOW_FREQUENCY:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((1.f - fabsf(y[n])), f_y);        break;    case AV_CHAN_BACK_CENTER:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((1.f - y[n]) * .5f, f_y);        break;    case AV_CHAN_BACK_LEFT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf(1.f - ((y[n] + 1.f) * .5f), f_y);        break;    case AV_CHAN_BACK_RIGHT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf(1.f - ((y[n] + 1.f) * .5f), f_y);        break;    case AV_CHAN_SIDE_LEFT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf(1.f - fabsf(y[n]), f_y);        break;    case AV_CHAN_SIDE_RIGHT:        for (int n = 0; n < rdft_size; n++)            factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf(1.f - fabsf(y[n]), f_y);        break;    default:        for (int n = 0; n < rdft_size; n++)            factor[n] = 1.f;        break;    }} static void do_transform(AVFilterContext *ctx, int ch){    AudioSurroundContext *s = ctx->priv;    float *sfactor = (float *)s->sfactors->extended_data[ch];    float *factor = (float *)s->factors->extended_data[ch];    float *omag = (float *)s->output_mag->extended_data[ch];    float *oph = (float *)s->output_ph->extended_data[ch];    float *dst = (float *)s->output->extended_data[ch];    const int rdft_size = s->rdft_size;    const float smooth = s->smooth;     if (smooth > 0.f) {        for (int n = 0; n < rdft_size; n++)            sfactor[n] = smooth * factor[n] + (1.f - smooth) * sfactor[n];         factor = sfactor;    }     for (int n = 0; n < rdft_size; n++)        omag[n] *= factor[n];     for (int n = 0; n < rdft_size; n++) {        const float mag = omag[n];        const float ph = oph[n];         TRANSFORM    }} static void stereo_copy(AVFilterContext *ctx, int ch, int chan){    AudioSurroundContext *s = ctx->priv;    float *omag = (float *)s->output_mag->extended_data[ch];    float *oph = (float *)s->output_ph->extended_data[ch];    const float *mag_total = s->mag_total;    const int rdft_size = s->rdft_size;    const float *c_phase = s->c_phase;    const float *l_phase = s->l_phase;    const float *r_phase = s->r_phase;    const float *lfe_mag = s->lfe_mag;    const float *c_mag = s->c_mag;     switch (chan) {    case AV_CHAN_FRONT_CENTER:        memcpy(omag, c_mag, rdft_size * sizeof(*omag));        break;    case AV_CHAN_LOW_FREQUENCY:        memcpy(omag, lfe_mag, rdft_size * sizeof(*omag));        break;    case AV_CHAN_FRONT_LEFT:    case AV_CHAN_FRONT_RIGHT:    case AV_CHAN_BACK_CENTER:    case AV_CHAN_BACK_LEFT:    case AV_CHAN_BACK_RIGHT:

Lines 1–500 of 1526.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t