3ea3e32e |
/*
* Copyright (c) 2012 Stefano Sabatini
*
* 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
*/
/**
* @file |
61230797 |
* audio to video multimedia filter |
3ea3e32e |
*/
|
1acd2f6b |
#include "libavutil/channel_layout.h" |
3ea3e32e |
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "avfilter.h"
#include "formats.h"
#include "audio.h"
#include "video.h" |
c17808ce |
#include "internal.h" |
3ea3e32e |
|
b7d34b32 |
enum ShowWavesMode {
MODE_POINT,
MODE_LINE,
MODE_NB,
};
|
3ea3e32e |
typedef struct {
const AVClass *class;
int w, h;
AVRational rate;
int buf_idx; |
a05a44e2 |
AVFrame *outpicref; |
3ea3e32e |
int req_fullfilled;
int n;
int sample_count_mod; |
b7d34b32 |
enum ShowWavesMode mode; |
3ea3e32e |
} ShowWavesContext;
#define OFFSET(x) offsetof(ShowWavesContext, x) |
42d621d1 |
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
3ea3e32e |
static const AVOption showwaves_options[] = { |
42d621d1 |
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS }, |
3c2e4c2a |
{ "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
{ "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
{ "line", "draw a line for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE}, .flags=FLAGS, .unit="mode"}, |
d46c1c72 |
{ "n", "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS }, |
3c2e4c2a |
{ "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
{ "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS }, |
b211607b |
{ NULL } |
3ea3e32e |
};
|
c17808ce |
AVFILTER_DEFINE_CLASS(showwaves); |
3ea3e32e |
static av_cold void uninit(AVFilterContext *ctx)
{
ShowWavesContext *showwaves = ctx->priv;
|
a05a44e2 |
av_frame_free(&showwaves->outpicref); |
3ea3e32e |
}
static int query_formats(AVFilterContext *ctx)
{
AVFilterFormats *formats = NULL;
AVFilterChannelLayouts *layouts = NULL;
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0]; |
e870a7dd |
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE }; |
3ea3e32e |
/* set input audio formats */
formats = ff_make_format_list(sample_fmts);
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &inlink->out_formats);
layouts = ff_all_channel_layouts();
if (!layouts)
return AVERROR(ENOMEM);
ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
formats = ff_all_samplerates();
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &inlink->out_samplerates);
/* set output video format */
formats = ff_make_format_list(pix_fmts);
if (!formats)
return AVERROR(ENOMEM);
ff_formats_ref(formats, &outlink->in_formats);
return 0;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
ShowWavesContext *showwaves = ctx->priv;
|
ac44e52f |
if (!showwaves->n) |
3ea3e32e |
showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
|
3c2e4c2a |
showwaves->buf_idx = 0; |
3ea3e32e |
outlink->w = showwaves->w;
outlink->h = showwaves->h;
outlink->sample_aspect_ratio = (AVRational){1,1};
outlink->frame_rate = av_div_q((AVRational){inlink->sample_rate,showwaves->n},
(AVRational){showwaves->w,1});
|
fda968aa |
av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d r:%f n:%d\n", |
3ea3e32e |
showwaves->w, showwaves->h, av_q2d(outlink->frame_rate), showwaves->n);
return 0;
}
|
e80be5a0 |
inline static int push_frame(AVFilterLink *outlink) |
3ea3e32e |
{
ShowWavesContext *showwaves = outlink->src->priv; |
e80be5a0 |
int ret; |
3ea3e32e |
|
e80be5a0 |
if ((ret = ff_filter_frame(outlink, showwaves->outpicref)) >= 0)
showwaves->req_fullfilled = 1; |
3ea3e32e |
showwaves->outpicref = NULL;
showwaves->buf_idx = 0; |
e80be5a0 |
return ret; |
3ea3e32e |
}
static int request_frame(AVFilterLink *outlink)
{
ShowWavesContext *showwaves = outlink->src->priv;
AVFilterLink *inlink = outlink->src->inputs[0];
int ret;
showwaves->req_fullfilled = 0;
do { |
1c600888 |
ret = ff_request_frame(inlink); |
3ea3e32e |
} while (!showwaves->req_fullfilled && ret >= 0);
if (ret == AVERROR_EOF && showwaves->outpicref)
push_frame(outlink);
return ret;
}
#define MAX_INT16 ((1<<15) -1)
|
a05a44e2 |
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) |
3ea3e32e |
{
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
ShowWavesContext *showwaves = ctx->priv; |
a05a44e2 |
const int nb_samples = insamples->nb_samples;
AVFrame *outpicref = showwaves->outpicref; |
3ea3e32e |
int linesize = outpicref ? outpicref->linesize[0] : 0;
int16_t *p = (int16_t *)insamples->data[0]; |
6347824d |
int nb_channels = inlink->channels; |
e80be5a0 |
int i, j, k, h, ret = 0; |
3ea3e32e |
const int n = showwaves->n;
const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
/* draw data in the buffer */
for (i = 0; i < nb_samples; i++) { |
5b10c5e7 |
if (!showwaves->outpicref) { |
3ea3e32e |
showwaves->outpicref = outpicref = |
a05a44e2 |
ff_get_video_buffer(outlink, outlink->w, outlink->h); |
b5436f4b |
if (!outpicref)
return AVERROR(ENOMEM); |
a05a44e2 |
outpicref->width = outlink->w;
outpicref->height = outlink->h; |
6fd0b55a |
outpicref->pts = insamples->pts +
av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
(AVRational){ 1, inlink->sample_rate },
outlink->time_base); |
3ea3e32e |
linesize = outpicref->linesize[0]; |
c6456599 |
for (j = 0; j < outlink->h; j++)
memset(outpicref->data[0] + j * linesize, 0, outlink->w); |
3ea3e32e |
}
for (j = 0; j < nb_channels; j++) {
h = showwaves->h/2 - av_rescale(*p++, showwaves->h/2, MAX_INT16); |
b7d34b32 |
switch (showwaves->mode) {
case MODE_POINT:
if (h >= 0 && h < outlink->h)
*(outpicref->data[0] + showwaves->buf_idx + h * linesize) += x;
break;
case MODE_LINE:
{
int start = showwaves->h/2, end = av_clip(h, 0, outlink->h-1);
if (start > end) FFSWAP(int16_t, start, end);
for (k = start; k < end; k++)
*(outpicref->data[0] + showwaves->buf_idx + k * linesize) += x;
break;
}
} |
3ea3e32e |
} |
b7d34b32 |
|
3ea3e32e |
showwaves->sample_count_mod++;
if (showwaves->sample_count_mod == n) {
showwaves->sample_count_mod = 0;
showwaves->buf_idx++;
}
if (showwaves->buf_idx == showwaves->w) |
e80be5a0 |
if ((ret = push_frame(outlink)) < 0)
break; |
e4eebc2d |
outpicref = showwaves->outpicref; |
3ea3e32e |
}
|
a05a44e2 |
av_frame_free(&insamples); |
e80be5a0 |
return ret; |
3ea3e32e |
}
|
2d9d4440 |
static const AVFilterPad showwaves_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
},
{ NULL }
};
static const AVFilterPad showwaves_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = config_output,
.request_frame = request_frame,
},
{ NULL }
};
|
325f6e0a |
AVFilter ff_avf_showwaves = { |
b211607b |
.name = "showwaves",
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
.uninit = uninit,
.query_formats = query_formats,
.priv_size = sizeof(ShowWavesContext),
.inputs = showwaves_inputs,
.outputs = showwaves_outputs,
.priv_class = &showwaves_class, |
3ea3e32e |
}; |