X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Faf_surround.c;h=11406786ee8fb5111b77afded35c12fe5c958af5;hb=3972ec28e76f08466279ad48355c45bcbe45b09f;hp=1f4a84d5e0ae79868f03e08d519dc48de395a2f7;hpb=4a69b18242dc39ee50993d55e985d3b2cbbef10b;p=ffmpeg diff --git a/libavfilter/af_surround.c b/libavfilter/af_surround.c index 1f4a84d5e0a..11406786ee8 100644 --- a/libavfilter/af_surround.c +++ b/libavfilter/af_surround.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/avassert.h" #include "libavutil/audio_fifo.h" #include "libavutil/channel_layout.h" #include "libavutil/opt.h" @@ -56,6 +57,7 @@ typedef struct AudioSurroundContext { float lfe_in; float lfe_out; int lfe_mode; + float angle; int win_size; int win_func; float overlap; @@ -312,9 +314,31 @@ static int config_output(AVFilterLink *outlink) return 0; } +static void stereo_transform(float *x, float *y, float angle) +{ + float reference, r, a; + + if (angle == 90.f) + return; + + reference = angle * M_PI / 180.f; + r = hypotf(*x, *y); + a = atan2f(*x, *y); + + if (fabsf(a) <= M_PI_4) + a *= reference / M_PI_2; + else + a = M_PI + 2 * (-2 * M_PI + reference) * (M_PI - fabsf(a)) * FFDIFFSIGN(a, 0) / (3 * M_PI); + + *x = av_clipf(sinf(a) * r, -1, 1); + *y = av_clipf(cosf(a) * r, -1, 1); +} + static void stereo_position(float a, float p, float *x, float *y) { - *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1); + av_assert2(a >= -1.f && a <= 1.f); + av_assert2(p >= 0.f && p <= M_PI); + *x = av_clipf(a+a*FFMAX(0, p*p-M_PI_2), -1, 1); *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1); } @@ -1042,7 +1066,7 @@ static void upmix_7_1_5_1(AVFilterContext *ctx, lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall; rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr; ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall; - rs_mag = powf(1.f - fabsf(xr), s->sl_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr; + rs_mag = powf(1.f - fabsf(xr), s->sr_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr; dstl[2 * n ] = fl_mag * cosf(fl_phase); dstl[2 * n + 1] = fl_mag * sinf(fl_phase); @@ -1088,7 +1112,7 @@ static void filter_stereo(AVFilterContext *ctx) float r_phase = atan2f(r_im, r_re); float phase_dif = fabsf(l_phase - r_phase); float mag_sum = l_mag + r_mag; - float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum; + float mag_dif = mag_sum < 0.000001 ? FFDIFFSIGN(l_mag, r_mag) : (l_mag - r_mag) / mag_sum; float mag_total = hypotf(l_mag, r_mag); float x, y; @@ -1096,6 +1120,7 @@ static void filter_stereo(AVFilterContext *ctx) phase_dif = 2 * M_PI - phase_dif; stereo_position(mag_dif, phase_dif, &x, &y); + stereo_transform(&x, &y, s->angle); s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n); } @@ -1123,7 +1148,7 @@ static void filter_surround(AVFilterContext *ctx) float r_phase = atan2f(r_im, r_re); float phase_dif = fabsf(l_phase - r_phase); float mag_sum = l_mag + r_mag; - float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum; + float mag_dif = mag_sum < 0.000001 ? FFDIFFSIGN(l_mag, r_mag) : (l_mag - r_mag) / mag_sum; float mag_total = hypotf(l_mag, r_mag); float x, y; @@ -1131,6 +1156,7 @@ static void filter_surround(AVFilterContext *ctx) phase_dif = 2 * M_PI - phase_dif; stereo_position(mag_dif, phase_dif, &x, &y); + stereo_transform(&x, &y, s->angle); s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n); } @@ -1157,7 +1183,7 @@ static void filter_2_1(AVFilterContext *ctx) float r_phase = atan2f(r_im, r_re); float phase_dif = fabsf(l_phase - r_phase); float mag_sum = l_mag + r_mag; - float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum; + float mag_dif = mag_sum < 0.000001 ? FFDIFFSIGN(l_mag, r_mag) : (l_mag - r_mag) / mag_sum; float mag_total = hypotf(l_mag, r_mag); float x, y; @@ -1165,6 +1191,7 @@ static void filter_2_1(AVFilterContext *ctx) phase_dif = 2 * M_PI - phase_dif; stereo_position(mag_dif, phase_dif, &x, &y); + stereo_transform(&x, &y, s->angle); s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n); } @@ -1200,8 +1227,8 @@ static void filter_5_0_side(AVFilterContext *ctx) float phase_difr = fabsf(fr_phase - sr_phase); float magl_sum = fl_mag + sl_mag; float magr_sum = fr_mag + sr_mag; - float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum; - float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum; + float mag_difl = magl_sum < 0.000001 ? FFDIFFSIGN(fl_mag, sl_mag) : (fl_mag - sl_mag) / magl_sum; + float mag_difr = magr_sum < 0.000001 ? FFDIFFSIGN(fr_mag, sr_mag) : (fr_mag - sr_mag) / magr_sum; float mag_totall = hypotf(fl_mag, sl_mag); float mag_totalr = hypotf(fr_mag, sr_mag); float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re); @@ -1259,8 +1286,8 @@ static void filter_5_1_side(AVFilterContext *ctx) float phase_difr = fabsf(fr_phase - sr_phase); float magl_sum = fl_mag + sl_mag; float magr_sum = fr_mag + sr_mag; - float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum; - float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum; + float mag_difl = magl_sum < 0.000001 ? FFDIFFSIGN(fl_mag, sl_mag) : (fl_mag - sl_mag) / magl_sum; + float mag_difr = magr_sum < 0.000001 ? FFDIFFSIGN(fr_mag, sr_mag) : (fr_mag - sr_mag) / magr_sum; float mag_totall = hypotf(fl_mag, sl_mag); float mag_totalr = hypotf(fr_mag, sr_mag); float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re); @@ -1318,8 +1345,8 @@ static void filter_5_1_back(AVFilterContext *ctx) float phase_difr = fabsf(fr_phase - br_phase); float magl_sum = fl_mag + bl_mag; float magr_sum = fr_mag + br_mag; - float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum; - float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum; + float mag_difl = magl_sum < 0.000001 ? FFDIFFSIGN(fl_mag, bl_mag) : (fl_mag - bl_mag) / magl_sum; + float mag_difr = magr_sum < 0.000001 ? FFDIFFSIGN(fr_mag, br_mag) : (fr_mag - br_mag) / magr_sum; float mag_totall = hypotf(fl_mag, bl_mag); float mag_totalr = hypotf(fr_mag, br_mag); float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re); @@ -1345,7 +1372,7 @@ static void filter_5_1_back(AVFilterContext *ctx) } } -static int init(AVFilterContext *ctx) +static av_cold int init(AVFilterContext *ctx) { AudioSurroundContext *s = ctx->priv; float overlap; @@ -1674,6 +1701,7 @@ static const AVOption surround_options[] = { { "lfe_mode", "set LFE channel mode", OFFSET(lfe_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "lfe_mode" }, { "add", "just add LFE channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 1, FLAGS, "lfe_mode" }, { "sub", "substract LFE channel with others", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 1, FLAGS, "lfe_mode" }, + { "angle", "set soundfield transform angle", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl=90}, 0, 360, FLAGS }, { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS }, { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS }, { "fl_in", "set front left channel input level", OFFSET(fl_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },