2011-02-21 13:02:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Brandon Mintern
|
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
|
|
|
*
|
2011-04-15 23:30:26 +03:00
|
|
|
* This file is part of Libav.
|
2011-02-21 13:02:57 +02:00
|
|
|
*
|
2011-04-15 23:30:26 +03:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2011-02-21 13:02:57 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2011-04-15 23:30:26 +03:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2011-02-21 13:02:57 +02:00
|
|
|
* 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
|
2011-04-15 23:30:26 +03:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2011-02-21 13:02:57 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* video fade filter
|
|
|
|
* based heavily on vf_negate.c by Bobby Bingham
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2013-02-25 23:21:29 +03:00
|
|
|
#include "libavutil/opt.h"
|
2011-02-21 13:02:57 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avfilter.h"
|
2012-05-30 11:12:55 +03:00
|
|
|
#include "formats.h"
|
2012-06-12 21:12:42 +03:00
|
|
|
#include "internal.h"
|
2012-05-19 11:37:56 +03:00
|
|
|
#include "video.h"
|
2011-02-21 13:02:57 +02:00
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
#define FADE_IN 0
|
|
|
|
#define FADE_OUT 1
|
|
|
|
|
2011-02-21 13:02:57 +02:00
|
|
|
typedef struct {
|
2013-02-25 23:21:29 +03:00
|
|
|
const AVClass *class;
|
|
|
|
int type;
|
2011-02-21 13:02:57 +02:00
|
|
|
int factor, fade_per_frame;
|
2013-02-25 23:21:29 +03:00
|
|
|
int start_frame, nb_frames;
|
|
|
|
unsigned int frame_index, stop_frame;
|
2011-02-21 13:02:57 +02:00
|
|
|
int hsub, vsub, bpp;
|
|
|
|
} FadeContext;
|
|
|
|
|
2013-03-13 10:26:39 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2011-02-21 13:02:57 +02:00
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
FadeContext *s = ctx->priv;
|
|
|
|
|
|
|
|
s->fade_per_frame = (1 << 16) / s->nb_frames;
|
|
|
|
if (s->type == FADE_IN) {
|
|
|
|
s->factor = 0;
|
|
|
|
} else if (s->type == FADE_OUT) {
|
|
|
|
s->fade_per_frame = -s->fade_per_frame;
|
|
|
|
s->factor = (1 << 16);
|
2011-02-21 13:02:57 +02:00
|
|
|
}
|
2013-03-18 22:44:36 +03:00
|
|
|
s->stop_frame = s->start_frame + s->nb_frames;
|
2011-02-21 13:02:57 +02:00
|
|
|
|
2012-06-25 07:31:38 +03:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE,
|
2011-02-21 13:02:57 +02:00
|
|
|
"type:%s start_frame:%d nb_frames:%d\n",
|
2013-03-18 22:44:36 +03:00
|
|
|
s->type == FADE_IN ? "in" : "out", s->start_frame,
|
|
|
|
s->nb_frames);
|
2011-02-21 13:02:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
2012-10-06 13:10:34 +03:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
|
|
|
|
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
|
|
|
|
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
|
|
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
|
|
|
|
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
|
|
|
|
AV_PIX_FMT_NONE
|
2011-02-21 13:02:57 +02:00
|
|
|
};
|
|
|
|
|
2012-05-30 11:12:55 +03:00
|
|
|
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
2011-02-21 13:02:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
2013-03-18 22:44:36 +03:00
|
|
|
FadeContext *s = inlink->dst->priv;
|
2012-10-06 14:29:37 +03:00
|
|
|
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
|
2011-02-21 13:02:57 +02:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
s->hsub = pixdesc->log2_chroma_w;
|
|
|
|
s->vsub = pixdesc->log2_chroma_h;
|
2011-02-21 13:02:57 +02:00
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
s->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
|
2011-02-21 13:02:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-11 22:40:28 +03:00
|
|
|
static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
|
|
|
|
int nb_jobs)
|
2011-02-21 13:02:57 +02:00
|
|
|
{
|
2013-05-11 22:40:28 +03:00
|
|
|
FadeContext *s = ctx->priv;
|
|
|
|
AVFrame *frame = arg;
|
|
|
|
int slice_h = frame->height / nb_jobs;
|
|
|
|
int slice_start = jobnr * slice_h;
|
|
|
|
int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
|
|
|
uint8_t *p = frame->data[0] + i * frame->linesize[0];
|
|
|
|
for (j = 0; j < frame->width * s->bpp; j++) {
|
|
|
|
/* s->factor is using 16 lower-order bits for decimal
|
|
|
|
* places. 32768 = 1 << 15, it is an integer representation
|
|
|
|
* of 0.5 and is for rounding. */
|
|
|
|
*p = (*p * s->factor + 32768) >> 16;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
|
|
|
|
int nb_jobs)
|
|
|
|
{
|
|
|
|
FadeContext *s = ctx->priv;
|
|
|
|
AVFrame *frame = arg;
|
|
|
|
int slice_h = FFALIGN(frame->height / nb_jobs, 1 << s->vsub);
|
|
|
|
int slice_start = jobnr * slice_h;
|
|
|
|
int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
|
2011-02-21 13:02:57 +02:00
|
|
|
int i, j, plane;
|
|
|
|
|
2013-05-11 22:40:28 +03:00
|
|
|
for (plane = 1; plane < 3; plane++) {
|
|
|
|
for (i = slice_start; i < slice_end; i++) {
|
|
|
|
uint8_t *p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane];
|
|
|
|
for (j = 0; j < frame->width >> s->hsub; j++) {
|
|
|
|
/* 8421367 = ((128 << 1) + 1) << 15. It is an integer
|
|
|
|
* representation of 128.5. The .5 is for rounding
|
|
|
|
* purposes. */
|
|
|
|
*p = ((*p - 128) * s->factor + 8421367) >> 16;
|
2011-02-21 13:02:57 +02:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2013-05-11 22:40:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
FadeContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->factor < UINT16_MAX) {
|
|
|
|
/* luma or rgb plane */
|
|
|
|
ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
|
|
|
|
FFMIN(frame->height, ctx->graph->nb_threads));
|
2011-02-21 13:02:57 +02:00
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
if (frame->data[1] && frame->data[2]) {
|
2011-02-21 13:02:57 +02:00
|
|
|
/* chroma planes */
|
2013-05-11 22:40:28 +03:00
|
|
|
ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
|
|
|
|
FFMIN(frame->height, ctx->graph->nb_threads));
|
2011-02-21 13:02:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-18 22:44:36 +03:00
|
|
|
if (s->frame_index >= s->start_frame &&
|
|
|
|
s->frame_index <= s->stop_frame)
|
|
|
|
s->factor += s->fade_per_frame;
|
|
|
|
s->factor = av_clip_uint16(s->factor);
|
|
|
|
s->frame_index++;
|
2012-07-14 10:25:33 +03:00
|
|
|
|
2012-11-27 09:49:45 +03:00
|
|
|
return ff_filter_frame(inlink->dst->outputs[0], frame);
|
2011-02-21 13:02:57 +02:00
|
|
|
}
|
|
|
|
|
2013-02-25 23:21:29 +03:00
|
|
|
#define OFFSET(x) offsetof(FadeContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
|
|
|
|
{ "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
|
|
|
|
{ "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
|
|
|
|
{ "start_frame", "Number of the first frame to which to apply the effect.",
|
|
|
|
OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
|
|
|
|
{ "nb_frames", "Number of frames to which the effect should be applied.",
|
|
|
|
OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass fade_class = {
|
|
|
|
.class_name = "fade",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_fade_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_props,
|
|
|
|
.get_video_buffer = ff_null_get_video_buffer,
|
2012-11-27 09:49:45 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-11-28 10:41:07 +03:00
|
|
|
.needs_writable = 1,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fade_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-28 09:44:24 +03:00
|
|
|
AVFilter ff_vf_fade = {
|
2011-02-21 13:02:57 +02:00
|
|
|
.name = "fade",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
|
|
|
|
.init = init,
|
|
|
|
.priv_size = sizeof(FadeContext),
|
2013-02-25 23:21:29 +03:00
|
|
|
.priv_class = &fade_class,
|
2011-02-21 13:02:57 +02:00
|
|
|
.query_formats = query_formats,
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
.inputs = avfilter_vf_fade_inputs,
|
|
|
|
.outputs = avfilter_vf_fade_outputs,
|
2013-05-11 22:40:28 +03:00
|
|
|
.flags = AVFILTER_FLAG_SLICE_THREADS,
|
2011-02-21 13:02:57 +02:00
|
|
|
};
|