LCOV - code coverage report
Current view: top level - src/libavfilter - avf_showvolume.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 157 0.0 %
Date: 2017-03-25 17:02:41 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Paul B Mahol
       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/avstring.h"
      22             : #include "libavutil/channel_layout.h"
      23             : #include "libavutil/eval.h"
      24             : #include "libavutil/intreadwrite.h"
      25             : #include "libavutil/opt.h"
      26             : #include "libavutil/parseutils.h"
      27             : #include "libavutil/xga_font_data.h"
      28             : #include "avfilter.h"
      29             : #include "formats.h"
      30             : #include "audio.h"
      31             : #include "video.h"
      32             : #include "internal.h"
      33             : 
      34             : static const char *const var_names[] = {   "VOLUME",   "CHANNEL",   "PEAK",        NULL };
      35             : enum                                   { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
      36             : 
      37             : typedef struct ShowVolumeContext {
      38             :     const AVClass *class;
      39             :     int w, h;
      40             :     int b;
      41             :     double f;
      42             :     AVRational frame_rate;
      43             :     char *color;
      44             :     int orientation;
      45             :     int step;
      46             : 
      47             :     AVFrame *out;
      48             :     AVExpr *c_expr;
      49             :     int draw_text;
      50             :     int draw_volume;
      51             :     double *values;
      52             :     uint32_t *color_lut;
      53             : } ShowVolumeContext;
      54             : 
      55             : #define OFFSET(x) offsetof(ShowVolumeContext, x)
      56             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      57             : 
      58             : static const AVOption showvolume_options[] = {
      59             :     { "rate", "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      60             :     { "r",    "set video rate",  OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
      61             :     { "b", "set border width",   OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
      62             :     { "w", "set channel width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
      63             :     { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
      64             :     { "f", "set fade",           OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.001, 1, FLAGS },
      65             :     { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
      66             :     { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
      67             :     { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
      68             :     { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
      69             :     {   "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
      70             :     {   "v", "vertical",   0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
      71             :     { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
      72             :     { NULL }
      73             : };
      74             : 
      75             : AVFILTER_DEFINE_CLASS(showvolume);
      76             : 
      77           0 : static av_cold int init(AVFilterContext *ctx)
      78             : {
      79           0 :     ShowVolumeContext *s = ctx->priv;
      80             :     int ret;
      81             : 
      82           0 :     if (s->color) {
      83           0 :         ret = av_expr_parse(&s->c_expr, s->color, var_names,
      84             :                             NULL, NULL, NULL, NULL, 0, ctx);
      85           0 :         if (ret < 0)
      86           0 :             return ret;
      87             :     }
      88             : 
      89           0 :     return 0;
      90             : }
      91             : 
      92           0 : static int query_formats(AVFilterContext *ctx)
      93             : {
      94           0 :     AVFilterFormats *formats = NULL;
      95           0 :     AVFilterChannelLayouts *layouts = NULL;
      96           0 :     AVFilterLink *inlink = ctx->inputs[0];
      97           0 :     AVFilterLink *outlink = ctx->outputs[0];
      98             :     static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
      99             :     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
     100             :     int ret;
     101             : 
     102           0 :     formats = ff_make_format_list(sample_fmts);
     103           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
     104           0 :         return ret;
     105             : 
     106           0 :     layouts = ff_all_channel_counts();
     107           0 :     if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
     108           0 :         return ret;
     109             : 
     110           0 :     formats = ff_all_samplerates();
     111           0 :     if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
     112           0 :         return ret;
     113             : 
     114           0 :     formats = ff_make_format_list(pix_fmts);
     115           0 :     if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
     116           0 :         return ret;
     117             : 
     118           0 :     return 0;
     119             : }
     120             : 
     121           0 : static int config_input(AVFilterLink *inlink)
     122             : {
     123           0 :     AVFilterContext *ctx = inlink->dst;
     124           0 :     ShowVolumeContext *s = ctx->priv;
     125             :     int nb_samples;
     126             : 
     127           0 :     nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
     128           0 :     inlink->partial_buf_size =
     129           0 :     inlink->min_samples =
     130           0 :     inlink->max_samples = nb_samples;
     131           0 :     s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
     132           0 :     if (!s->values)
     133           0 :         return AVERROR(ENOMEM);
     134             : 
     135           0 :     s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->channels);
     136           0 :     if (!s->color_lut)
     137           0 :         return AVERROR(ENOMEM);
     138             : 
     139           0 :     return 0;
     140             : }
     141             : 
     142           0 : static int config_output(AVFilterLink *outlink)
     143             : {
     144           0 :     ShowVolumeContext *s = outlink->src->priv;
     145           0 :     AVFilterLink *inlink = outlink->src->inputs[0];
     146             :     int ch;
     147             : 
     148           0 :     if (s->orientation) {
     149           0 :         outlink->h = s->w;
     150           0 :         outlink->w = s->h * inlink->channels + (inlink->channels - 1) * s->b;
     151             :     } else {
     152           0 :         outlink->w = s->w;
     153           0 :         outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
     154             :     }
     155             : 
     156           0 :     outlink->sample_aspect_ratio = (AVRational){1,1};
     157           0 :     outlink->frame_rate = s->frame_rate;
     158             : 
     159           0 :     for (ch = 0; ch < inlink->channels; ch++) {
     160             :         int i;
     161             : 
     162           0 :         for (i = 0; i < s->w; i++) {
     163           0 :             float max = i / (float)(s->w - 1);
     164             : 
     165           0 :             s->values[ch * VAR_VARS_NB + VAR_PEAK] = max;
     166           0 :             s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
     167           0 :             s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch;
     168           0 :             s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL);
     169             :         }
     170             :     }
     171             : 
     172           0 :     return 0;
     173             : }
     174             : 
     175           0 : static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
     176             : {
     177             :     const uint8_t *font;
     178             :     int font_height;
     179             :     int i;
     180             : 
     181           0 :     font = avpriv_cga_font,   font_height =  8;
     182             : 
     183           0 :     for (i = 0; txt[i]; i++) {
     184             :         int char_y, mask;
     185             : 
     186           0 :         if (o) {
     187           0 :             for (char_y = font_height - 1; char_y >= 0; char_y--) {
     188           0 :                 uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
     189           0 :                 for (mask = 0x80; mask; mask >>= 1) {
     190           0 :                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
     191           0 :                         AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
     192           0 :                     p += pic->linesize[0];
     193             :                 }
     194             :             }
     195             :         } else {
     196           0 :             uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
     197           0 :             for (char_y = 0; char_y < font_height; char_y++) {
     198           0 :                 for (mask = 0x80; mask; mask >>= 1) {
     199           0 :                     if (font[txt[i] * font_height + char_y] & mask)
     200           0 :                         AV_WN32(p, ~AV_RN32(p));
     201           0 :                     p += 4;
     202             :                 }
     203           0 :                 p += pic->linesize[0] - 8 * 4;
     204             :             }
     205             :         }
     206             :     }
     207           0 : }
     208             : 
     209           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
     210             : {
     211           0 :     AVFilterContext *ctx = inlink->dst;
     212           0 :     AVFilterLink *outlink = ctx->outputs[0];
     213           0 :     ShowVolumeContext *s = ctx->priv;
     214           0 :     const int step = s->step;
     215             :     int c, i, j, k;
     216             :     AVFrame *out;
     217             : 
     218           0 :     if (!s->out || s->out->width  != outlink->w ||
     219           0 :                    s->out->height != outlink->h) {
     220           0 :         av_frame_free(&s->out);
     221           0 :         s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     222           0 :         if (!s->out) {
     223           0 :             av_frame_free(&insamples);
     224           0 :             return AVERROR(ENOMEM);
     225             :         }
     226             : 
     227           0 :         for (i = 0; i < outlink->h; i++)
     228           0 :             memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
     229             :     }
     230           0 :     s->out->pts = insamples->pts;
     231             : 
     232           0 :     for (j = 0; j < outlink->h; j++) {
     233           0 :         uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
     234           0 :         for (k = 0; k < outlink->w; k++) {
     235           0 :             dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
     236           0 :             dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
     237           0 :             dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
     238           0 :             dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, 0);
     239             :         }
     240             :     }
     241             : 
     242           0 :     if (s->orientation) {
     243           0 :         for (c = 0; c < inlink->channels; c++) {
     244           0 :             float *src = (float *)insamples->extended_data[c];
     245           0 :             uint32_t *lut = s->color_lut + s->w * c;
     246           0 :             float max = 0;
     247             : 
     248           0 :             for (i = 0; i < insamples->nb_samples; i++)
     249           0 :                 max = FFMAX(max, src[i]);
     250             : 
     251           0 :             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
     252           0 :             max = av_clipf(max, 0, 1);
     253             : 
     254           0 :             for (j = outlink->h - outlink->h * max; j < s->w; j++) {
     255           0 :                 uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
     256           0 :                 for (k = 0; k < s->h; k++) {
     257           0 :                     AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
     258           0 :                     if (j & step)
     259           0 :                         j += step;
     260             :                 }
     261             :             }
     262             : 
     263           0 :             if (s->h >= 8 && s->draw_text) {
     264           0 :                 const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
     265           0 :                 if (!channel_name)
     266           0 :                     continue;
     267           0 :                 drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
     268             :             }
     269             :         }
     270             :     } else {
     271           0 :         for (c = 0; c < inlink->channels; c++) {
     272           0 :             float *src = (float *)insamples->extended_data[c];
     273           0 :             uint32_t *lut = s->color_lut + s->w * c;
     274           0 :             float max = 0;
     275             : 
     276           0 :             for (i = 0; i < insamples->nb_samples; i++)
     277           0 :                 max = FFMAX(max, src[i]);
     278             : 
     279           0 :             s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
     280           0 :             max = av_clipf(max, 0, 1);
     281             : 
     282           0 :             for (j = 0; j < s->h; j++) {
     283           0 :                 uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
     284             : 
     285           0 :                 for (k = 0; k < s->w * max; k++) {
     286           0 :                     AV_WN32A(dst + k * 4, lut[k]);
     287           0 :                     if (k & step)
     288           0 :                         k += step;
     289             :                 }
     290             :             }
     291             : 
     292           0 :             if (s->h >= 8 && s->draw_text) {
     293           0 :                 const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
     294           0 :                 if (!channel_name)
     295           0 :                     continue;
     296           0 :                 drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
     297             :             }
     298             :         }
     299             :     }
     300             : 
     301           0 :     av_frame_free(&insamples);
     302           0 :     out = av_frame_clone(s->out);
     303           0 :     if (!out)
     304           0 :         return AVERROR(ENOMEM);
     305           0 :     av_frame_make_writable(out);
     306             : 
     307           0 :     for (c = 0; c < inlink->channels && s->draw_volume; c++) {
     308             :         char buf[16];
     309           0 :         if (s->orientation) {
     310           0 :             if (s->h >= 8) {
     311           0 :                 snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
     312           0 :                 drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
     313             :             }
     314             :         } else {
     315           0 :             if (s->h >= 8) {
     316           0 :                 snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
     317           0 :                 drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
     318             :             }
     319             :         }
     320             :     }
     321             : 
     322           0 :     return ff_filter_frame(outlink, out);
     323             : }
     324             : 
     325           0 : static av_cold void uninit(AVFilterContext *ctx)
     326             : {
     327           0 :     ShowVolumeContext *s = ctx->priv;
     328             : 
     329           0 :     av_frame_free(&s->out);
     330           0 :     av_expr_free(s->c_expr);
     331           0 :     av_freep(&s->values);
     332           0 :     av_freep(&s->color_lut);
     333           0 : }
     334             : 
     335             : static const AVFilterPad showvolume_inputs[] = {
     336             :     {
     337             :         .name         = "default",
     338             :         .type         = AVMEDIA_TYPE_AUDIO,
     339             :         .config_props = config_input,
     340             :         .filter_frame = filter_frame,
     341             :     },
     342             :     { NULL }
     343             : };
     344             : 
     345             : static const AVFilterPad showvolume_outputs[] = {
     346             :     {
     347             :         .name         = "default",
     348             :         .type         = AVMEDIA_TYPE_VIDEO,
     349             :         .config_props = config_output,
     350             :     },
     351             :     { NULL }
     352             : };
     353             : 
     354             : AVFilter ff_avf_showvolume = {
     355             :     .name          = "showvolume",
     356             :     .description   = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
     357             :     .init          = init,
     358             :     .uninit        = uninit,
     359             :     .query_formats = query_formats,
     360             :     .priv_size     = sizeof(ShowVolumeContext),
     361             :     .inputs        = showvolume_inputs,
     362             :     .outputs       = showvolume_outputs,
     363             :     .priv_class    = &showvolume_class,
     364             : };

Generated by: LCOV version 1.13