25
25
* based heavily on vf_negate.c by Bobby Bingham
28
#include "libavutil/common.h"
28
29
#include "libavutil/pixdesc.h"
29
30
#include "avfilter.h"
32
36
int factor, fade_per_frame;
34
38
int hsub, vsub, bpp;
37
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
41
static av_cold int init(AVFilterContext *ctx, const char *args)
39
43
FadeContext *fade = ctx->priv;
40
44
unsigned int nb_frames;
62
66
fade->stop_frame = fade->start_frame + nb_frames;
64
av_log(ctx, AV_LOG_INFO,
68
av_log(ctx, AV_LOG_VERBOSE,
65
69
"type:%s start_frame:%d nb_frames:%d\n",
66
70
in_out, fade->start_frame, nb_frames);
70
74
static int query_formats(AVFilterContext *ctx)
72
static const enum PixelFormat pix_fmts[] = {
73
PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
74
PIX_FMT_YUV411P, PIX_FMT_YUV410P,
75
PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
76
PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
77
PIX_FMT_RGB24, PIX_FMT_BGR24,
76
static const enum AVPixelFormat pix_fmts[] = {
77
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
78
AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
79
AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
80
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
81
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
81
avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
85
ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
85
89
static int config_props(AVFilterLink *inlink)
87
91
FadeContext *fade = inlink->dst->priv;
88
const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
92
const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
90
94
fade->hsub = pixdesc->log2_chroma_w;
91
95
fade->vsub = pixdesc->log2_chroma_h;
97
static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
101
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
99
103
FadeContext *fade = inlink->dst->priv;
100
AVFilterBufferRef *outpic = inlink->cur_buf;
104
107
if (fade->factor < UINT16_MAX) {
105
108
/* luma or rgb plane */
106
for (i = 0; i < h; i++) {
107
p = outpic->data[0] + (y+i) * outpic->linesize[0];
109
for (i = 0; i < frame->video->h; i++) {
110
p = frame->data[0] + i * frame->linesize[0];
108
111
for (j = 0; j < inlink->w * fade->bpp; j++) {
109
112
/* fade->factor is using 16 lower-order bits for decimal
110
113
* places. 32768 = 1 << 15, it is an integer representation
117
if (outpic->data[1] && outpic->data[2]) {
120
if (frame->data[1] && frame->data[2]) {
118
121
/* chroma planes */
119
122
for (plane = 1; plane < 3; plane++) {
120
for (i = 0; i < h; i++) {
121
p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
123
for (i = 0; i < frame->video->h; i++) {
124
p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
122
125
for (j = 0; j < inlink->w >> fade->hsub; j++) {
123
126
/* 8421367 = ((128 << 1) + 1) << 15. It is an integer
124
127
* representation of 128.5. The .5 is for rounding
134
avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
137
static void end_frame(AVFilterLink *inlink)
139
FadeContext *fade = inlink->dst->priv;
141
avfilter_end_frame(inlink->dst->outputs[0]);
143
137
if (fade->frame_index >= fade->start_frame &&
144
138
fade->frame_index <= fade->stop_frame)
145
139
fade->factor += fade->fade_per_frame;
146
140
fade->factor = av_clip_uint16(fade->factor);
147
141
fade->frame_index++;
143
return ff_filter_frame(inlink->dst->outputs[0], frame);
146
static const AVFilterPad avfilter_vf_fade_inputs[] = {
149
.type = AVMEDIA_TYPE_VIDEO,
150
.config_props = config_props,
151
.get_video_buffer = ff_null_get_video_buffer,
152
.filter_frame = filter_frame,
153
.min_perms = AV_PERM_READ | AV_PERM_WRITE,
154
.rej_perms = AV_PERM_PRESERVE,
159
static const AVFilterPad avfilter_vf_fade_outputs[] = {
162
.type = AVMEDIA_TYPE_VIDEO,
150
167
AVFilter avfilter_vf_fade = {
152
169
.description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
154
171
.priv_size = sizeof(FadeContext),
155
172
.query_formats = query_formats,
157
.inputs = (AVFilterPad[]) {{ .name = "default",
158
.type = AVMEDIA_TYPE_VIDEO,
159
.config_props = config_props,
160
.get_video_buffer = avfilter_null_get_video_buffer,
161
.start_frame = avfilter_null_start_frame,
162
.draw_slice = draw_slice,
163
.end_frame = end_frame,
164
.min_perms = AV_PERM_READ | AV_PERM_WRITE,
165
.rej_perms = AV_PERM_PRESERVE, },
167
.outputs = (AVFilterPad[]) {{ .name = "default",
168
.type = AVMEDIA_TYPE_VIDEO, },
174
.inputs = avfilter_vf_fade_inputs,
175
.outputs = avfilter_vf_fade_outputs,