LCOV - code coverage report
Current view: top level - src/libavfilter - af_crystalizer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 50 0.0 %
Date: 2017-01-28 02:43:52 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 The FFmpeg Project
       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             : typedef struct CrystalizerContext {
      28             :     const AVClass *class;
      29             :     float mult;
      30             :     int clip;
      31             :     AVFrame *prev;
      32             : } CrystalizerContext;
      33             : 
      34             : #define OFFSET(x) offsetof(CrystalizerContext, x)
      35             : #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      36             : 
      37             : static const AVOption crystalizer_options[] = {
      38             :     { "i", "set intensity",    OFFSET(mult), AV_OPT_TYPE_FLOAT, {.dbl=2.0}, 0, 10, A },
      39             :     { "c", "enable clipping",  OFFSET(clip), AV_OPT_TYPE_BOOL,  {.i64=1},   0,  1, A },
      40             :     { NULL }
      41             : };
      42             : 
      43             : AVFILTER_DEFINE_CLASS(crystalizer);
      44             : 
      45           0 : static int query_formats(AVFilterContext *ctx)
      46             : {
      47           0 :     AVFilterFormats *formats = NULL;
      48           0 :     AVFilterChannelLayouts *layouts = NULL;
      49             :     int ret;
      50             : 
      51           0 :     if ((ret = ff_add_format        (&formats, AV_SAMPLE_FMT_FLT )) < 0 ||
      52           0 :         (ret = ff_set_common_formats(ctx     , formats           )) < 0)
      53           0 :         return ret;
      54             : 
      55           0 :     layouts = ff_all_channel_counts();
      56           0 :     if (!layouts)
      57           0 :         return AVERROR(ENOMEM);
      58             : 
      59           0 :     ret = ff_set_common_channel_layouts(ctx, layouts);
      60           0 :     if (ret < 0)
      61           0 :         return ret;
      62             : 
      63           0 :     formats = ff_all_samplerates();
      64           0 :     return ff_set_common_samplerates(ctx, formats);
      65             : }
      66             : 
      67           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
      68             : {
      69           0 :     AVFilterContext *ctx = inlink->dst;
      70           0 :     AVFilterLink *outlink = ctx->outputs[0];
      71           0 :     CrystalizerContext *s = ctx->priv;
      72           0 :     const float *src = (const float *)in->data[0];
      73           0 :     const float mult = s->mult;
      74             :     AVFrame *out;
      75             :     float *dst, *prv;
      76             :     int n, c;
      77             : 
      78           0 :     if (!s->prev) {
      79           0 :         s->prev = ff_get_audio_buffer(inlink, 1);
      80           0 :         if (!s->prev) {
      81           0 :             av_frame_free(&in);
      82           0 :             return AVERROR(ENOMEM);
      83             :         }
      84             :     }
      85             : 
      86           0 :     if (av_frame_is_writable(in)) {
      87           0 :         out = in;
      88             :     } else {
      89           0 :         out = ff_get_audio_buffer(inlink, in->nb_samples);
      90           0 :         if (!out) {
      91           0 :             av_frame_free(&in);
      92           0 :             return AVERROR(ENOMEM);
      93             :         }
      94           0 :         av_frame_copy_props(out, in);
      95             :     }
      96             : 
      97           0 :     dst = (float *)out->data[0];
      98           0 :     prv = (float *)s->prev->data[0];
      99             : 
     100           0 :     for (n = 0; n < in->nb_samples; n++) {
     101           0 :         for (c = 0; c < in->channels; c++) {
     102           0 :             float current = src[c];
     103             : 
     104           0 :             dst[c] = current + (current - prv[c]) * mult;
     105           0 :             prv[c] = current;
     106           0 :             if (s->clip) {
     107           0 :                 dst[c] = av_clipf(dst[c], -1, 1);
     108             :             }
     109             :         }
     110           0 :         dst += c;
     111           0 :         src += c;
     112             :     }
     113             : 
     114           0 :     if (out != in)
     115           0 :         av_frame_free(&in);
     116             : 
     117           0 :     return ff_filter_frame(outlink, out);
     118             : }
     119             : 
     120           0 : static av_cold void uninit(AVFilterContext *ctx)
     121             : {
     122           0 :     CrystalizerContext *s = ctx->priv;
     123             : 
     124           0 :     av_frame_free(&s->prev);
     125           0 : }
     126             : 
     127             : static const AVFilterPad inputs[] = {
     128             :     {
     129             :         .name         = "default",
     130             :         .type         = AVMEDIA_TYPE_AUDIO,
     131             :         .filter_frame = filter_frame,
     132             :     },
     133             :     { NULL }
     134             : };
     135             : 
     136             : static const AVFilterPad outputs[] = {
     137             :     {
     138             :         .name = "default",
     139             :         .type = AVMEDIA_TYPE_AUDIO,
     140             :     },
     141             :     { NULL }
     142             : };
     143             : 
     144             : AVFilter ff_af_crystalizer = {
     145             :     .name           = "crystalizer",
     146             :     .description    = NULL_IF_CONFIG_SMALL("Simple expand audio dynamic range filter."),
     147             :     .query_formats  = query_formats,
     148             :     .priv_size      = sizeof(CrystalizerContext),
     149             :     .priv_class     = &crystalizer_class,
     150             :     .uninit         = uninit,
     151             :     .inputs         = inputs,
     152             :     .outputs        = outputs,
     153             : };

Generated by: LCOV version 1.12