LCOV - code coverage report
Current view: top level - libavfilter - af_haas.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 76 0.0 %
Date: 2017-12-16 13:57:32 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2001-2010 Vladimir Sadovnikov
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "libavutil/channel_layout.h"
      22             : #include "libavutil/opt.h"
      23             : #include "avfilter.h"
      24             : #include "audio.h"
      25             : #include "formats.h"
      26             : 
      27             : #define MAX_HAAS_DELAY 40
      28             : 
      29             : typedef struct HaasContext {
      30             :     const AVClass *class;
      31             : 
      32             :     int par_m_source;
      33             :     double par_delay0;
      34             :     double par_delay1;
      35             :     int par_phase0;
      36             :     int par_phase1;
      37             :     int par_middle_phase;
      38             :     double par_side_gain;
      39             :     double par_gain0;
      40             :     double par_gain1;
      41             :     double par_balance0;
      42             :     double par_balance1;
      43             :     double level_in;
      44             :     double level_out;
      45             : 
      46             :     double *buffer;
      47             :     size_t buffer_size;
      48             :     uint32_t write_ptr;
      49             :     uint32_t delay[2];
      50             :     double balance_l[2];
      51             :     double balance_r[2];
      52             :     double phase0;
      53             :     double phase1;
      54             : } HaasContext;
      55             : 
      56             : #define OFFSET(x) offsetof(HaasContext, x)
      57             : #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      58             : 
      59             : static const AVOption haas_options[] = {
      60             :     { "level_in",      "set level in",      OFFSET(level_in),         AV_OPT_TYPE_DOUBLE,  {.dbl=1}, 0.015625,  64, A },
      61             :     { "level_out",     "set level out",     OFFSET(level_out),        AV_OPT_TYPE_DOUBLE,  {.dbl=1}, 0.015625,  64, A },
      62             :     { "side_gain",     "set side gain",     OFFSET(par_side_gain),    AV_OPT_TYPE_DOUBLE,  {.dbl=1}, 0.015625,  64, A },
      63             :     { "middle_source", "set middle source", OFFSET(par_m_source),     AV_OPT_TYPE_INT,     {.i64=2},        0,   3, A, "source" },
      64             :     {   "left",        0,                   0,                        AV_OPT_TYPE_CONST,   {.i64=0},        0,   0, A, "source" },
      65             :     {   "right",       0,                   0,                        AV_OPT_TYPE_CONST,   {.i64=1},        0,   0, A, "source" },
      66             :     {   "mid",         "L+R",               0,                        AV_OPT_TYPE_CONST,   {.i64=2},        0,   0, A, "source" },
      67             :     {   "side",        "L-R",               0,                        AV_OPT_TYPE_CONST,   {.i64=3},        0,   0, A, "source" },
      68             :     { "middle_phase",  "set middle phase",  OFFSET(par_middle_phase), AV_OPT_TYPE_BOOL,    {.i64=0},        0,   1, A },
      69             :     { "left_delay",    "set left delay",    OFFSET(par_delay0),       AV_OPT_TYPE_DOUBLE,  {.dbl=2.05},     0,  MAX_HAAS_DELAY, A },
      70             :     { "left_balance",  "set left balance",  OFFSET(par_balance0),     AV_OPT_TYPE_DOUBLE,  {.dbl=-1.0},    -1,   1, A },
      71             :     { "left_gain",     "set left gain",     OFFSET(par_gain0),        AV_OPT_TYPE_DOUBLE,  {.dbl=1}, 0.015625,  64, A },
      72             :     { "left_phase",    "set left phase",    OFFSET(par_phase0),       AV_OPT_TYPE_BOOL,    {.i64=0},        0,   1, A },
      73             :     { "right_delay",   "set right delay",   OFFSET(par_delay1),       AV_OPT_TYPE_DOUBLE,  {.dbl=2.12},     0,  MAX_HAAS_DELAY, A },
      74             :     { "right_balance", "set right balance", OFFSET(par_balance1),     AV_OPT_TYPE_DOUBLE,  {.dbl=1},       -1,   1, A },
      75             :     { "right_gain",    "set right gain",    OFFSET(par_gain1),        AV_OPT_TYPE_DOUBLE,  {.dbl=1}, 0.015625,  64, A },
      76             :     { "right_phase",   "set right phase",   OFFSET(par_phase1),       AV_OPT_TYPE_BOOL,    {.i64=1},        0,   1, A },
      77             :     { NULL }
      78             : };
      79             : 
      80             : AVFILTER_DEFINE_CLASS(haas);
      81             : 
      82           0 : static int query_formats(AVFilterContext *ctx)
      83             : {
      84           0 :     AVFilterFormats *formats = NULL;
      85           0 :     AVFilterChannelLayouts *layout = NULL;
      86             :     int ret;
      87             : 
      88           0 :     if ((ret = ff_add_format                 (&formats, AV_SAMPLE_FMT_DBL  )) < 0 ||
      89           0 :         (ret = ff_set_common_formats         (ctx     , formats            )) < 0 ||
      90           0 :         (ret = ff_add_channel_layout         (&layout , AV_CH_LAYOUT_STEREO)) < 0 ||
      91           0 :         (ret = ff_set_common_channel_layouts (ctx     , layout             )) < 0)
      92           0 :         return ret;
      93             : 
      94           0 :     formats = ff_all_samplerates();
      95           0 :     return ff_set_common_samplerates(ctx, formats);
      96             : }
      97             : 
      98           0 : static int config_input(AVFilterLink *inlink)
      99             : {
     100           0 :     AVFilterContext *ctx = inlink->dst;
     101           0 :     HaasContext *s = ctx->priv;
     102           0 :     size_t min_buf_size = (size_t)(inlink->sample_rate * MAX_HAAS_DELAY * 0.001);
     103           0 :     size_t new_buf_size = 1;
     104             : 
     105           0 :     while (new_buf_size < min_buf_size)
     106           0 :         new_buf_size <<= 1;
     107             : 
     108           0 :     av_freep(&s->buffer);
     109           0 :     s->buffer = av_calloc(new_buf_size, sizeof(*s->buffer));
     110           0 :     if (!s->buffer)
     111           0 :         return AVERROR(ENOMEM);
     112             : 
     113           0 :     s->buffer_size = new_buf_size;
     114           0 :     s->write_ptr = 0;
     115             : 
     116           0 :     s->delay[0] = (uint32_t)(s->par_delay0 * 0.001 * inlink->sample_rate);
     117           0 :     s->delay[1] = (uint32_t)(s->par_delay1 * 0.001 * inlink->sample_rate);
     118             : 
     119           0 :     s->phase0 = s->par_phase0 ? 1.0 : -1.0;
     120           0 :     s->phase1 = s->par_phase1 ? 1.0 : -1.0;
     121             : 
     122           0 :     s->balance_l[0] = (s->par_balance0 + 1) / 2 * s->par_gain0 * s->phase0;
     123           0 :     s->balance_r[0] = (1.0 - (s->par_balance0 + 1) / 2) * (s->par_gain0) * s->phase0;
     124           0 :     s->balance_l[1] = (s->par_balance1 + 1) / 2 * s->par_gain1 * s->phase1;
     125           0 :     s->balance_r[1] = (1.0 - (s->par_balance1 + 1) / 2) * (s->par_gain1) * s->phase1;
     126             : 
     127           0 :     return 0;
     128             : }
     129             : 
     130           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     131             : {
     132           0 :     AVFilterContext *ctx = inlink->dst;
     133           0 :     AVFilterLink *outlink = ctx->outputs[0];
     134           0 :     HaasContext *s = ctx->priv;
     135           0 :     const double *src = (const double *)in->data[0];
     136           0 :     const double level_in = s->level_in;
     137           0 :     const double level_out = s->level_out;
     138           0 :     const uint32_t mask = s->buffer_size - 1;
     139           0 :     double *buffer = s->buffer;
     140             :     AVFrame *out;
     141             :     double *dst;
     142             :     int n;
     143             : 
     144           0 :     if (av_frame_is_writable(in)) {
     145           0 :         out = in;
     146             :     } else {
     147           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
     148           0 :         if (!out) {
     149           0 :             av_frame_free(&in);
     150           0 :             return AVERROR(ENOMEM);
     151             :         }
     152           0 :         av_frame_copy_props(out, in);
     153             :     }
     154           0 :     dst = (double *)out->data[0];
     155             : 
     156           0 :     for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
     157             :         double mid, side[2], side_l, side_r;
     158             :         uint32_t s0_ptr, s1_ptr;
     159             : 
     160           0 :         switch (s->par_m_source) {
     161           0 :         case 0: mid = src[0]; break;
     162           0 :         case 1: mid = src[1]; break;
     163           0 :         case 2: mid = (src[0] + src[1]) * 0.5; break;
     164           0 :         case 3: mid = (src[0] - src[1]) * 0.5; break;
     165             :         }
     166             : 
     167           0 :         mid *= level_in;
     168             : 
     169           0 :         buffer[s->write_ptr] = mid;
     170             : 
     171           0 :         s0_ptr = (s->write_ptr + s->buffer_size - s->delay[0]) & mask;
     172           0 :         s1_ptr = (s->write_ptr + s->buffer_size - s->delay[1]) & mask;
     173             : 
     174           0 :         if (s->par_middle_phase)
     175           0 :             mid = -mid;
     176             : 
     177           0 :         side[0] = buffer[s0_ptr] * s->par_side_gain;
     178           0 :         side[1] = buffer[s1_ptr] * s->par_side_gain;
     179           0 :         side_l  = side[0] * s->balance_l[0] - side[1] * s->balance_l[1];
     180           0 :         side_r  = side[1] * s->balance_r[1] - side[0] * s->balance_r[0];
     181             : 
     182           0 :         dst[0] = (mid + side_l) * level_out;
     183           0 :         dst[1] = (mid + side_r) * level_out;
     184             : 
     185           0 :         s->write_ptr = (s->write_ptr + 1) & mask;
     186             :     }
     187             : 
     188           0 :     if (out != in)
     189           0 :         av_frame_free(&in);
     190           0 :     return ff_filter_frame(outlink, out);
     191             : }
     192             : 
     193           0 : static av_cold void uninit(AVFilterContext *ctx)
     194             : {
     195           0 :     HaasContext *s = ctx->priv;
     196             : 
     197           0 :     av_freep(&s->buffer);
     198           0 :     s->buffer_size = 0;
     199           0 : }
     200             : 
     201             : static const AVFilterPad inputs[] = {
     202             :     {
     203             :         .name         = "default",
     204             :         .type         = AVMEDIA_TYPE_AUDIO,
     205             :         .filter_frame = filter_frame,
     206             :         .config_props = config_input,
     207             :     },
     208             :     { NULL }
     209             : };
     210             : 
     211             : static const AVFilterPad outputs[] = {
     212             :     {
     213             :         .name = "default",
     214             :         .type = AVMEDIA_TYPE_AUDIO,
     215             :     },
     216             :     { NULL }
     217             : };
     218             : 
     219             : AVFilter ff_af_haas = {
     220             :     .name           = "haas",
     221             :     .description    = NULL_IF_CONFIG_SMALL("Apply Haas Stereo Enhancer."),
     222             :     .query_formats  = query_formats,
     223             :     .priv_size      = sizeof(HaasContext),
     224             :     .priv_class     = &haas_class,
     225             :     .uninit         = uninit,
     226             :     .inputs         = inputs,
     227             :     .outputs        = outputs,
     228             : };

Generated by: LCOV version 1.13