Libav
vf_fade.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Brandon Mintern
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/common.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 #define FADE_IN 0
37 #define FADE_OUT 1
38 
39 typedef struct FadeContext {
40  const AVClass *class;
41  int type;
44  unsigned int frame_index, stop_frame;
45  int hsub, vsub, bpp;
46 } FadeContext;
47 
48 static av_cold int init(AVFilterContext *ctx)
49 {
50  FadeContext *s = ctx->priv;
51 
52  s->fade_per_frame = (1 << 16) / s->nb_frames;
53  if (s->type == FADE_IN) {
54  s->factor = 0;
55  } else if (s->type == FADE_OUT) {
57  s->factor = (1 << 16);
58  }
59  s->stop_frame = s->start_frame + s->nb_frames;
60 
62  "type:%s start_frame:%d nb_frames:%d\n",
63  s->type == FADE_IN ? "in" : "out", s->start_frame,
64  s->nb_frames);
65  return 0;
66 }
67 
69 {
70  static const enum AVPixelFormat pix_fmts[] = {
77  };
78 
80  return 0;
81 }
82 
83 static int config_props(AVFilterLink *inlink)
84 {
85  FadeContext *s = inlink->dst->priv;
86  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
87 
88  s->hsub = pixdesc->log2_chroma_w;
89  s->vsub = pixdesc->log2_chroma_h;
90 
91  s->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
92  return 0;
93 }
94 
95 static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr,
96  int nb_jobs)
97 {
98  FadeContext *s = ctx->priv;
99  AVFrame *frame = arg;
100  int slice_h = frame->height / nb_jobs;
101  int slice_start = jobnr * slice_h;
102  int slice_end = (jobnr == nb_jobs - 1) ? frame->height : (jobnr + 1) * slice_h;
103  int i, j;
104 
105  for (i = slice_start; i < slice_end; i++) {
106  uint8_t *p = frame->data[0] + i * frame->linesize[0];
107  for (j = 0; j < frame->width * s->bpp; j++) {
108  /* s->factor is using 16 lower-order bits for decimal
109  * places. 32768 = 1 << 15, it is an integer representation
110  * of 0.5 and is for rounding. */
111  *p = (*p * s->factor + 32768) >> 16;
112  p++;
113  }
114  }
115 
116  return 0;
117 }
118 
119 static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr,
120  int nb_jobs)
121 {
122  FadeContext *s = ctx->priv;
123  AVFrame *frame = arg;
124  int slice_h = FFALIGN(frame->height / nb_jobs, 1 << s->vsub);
125  int slice_start = jobnr * slice_h;
126  int slice_end = (jobnr == nb_jobs - 1) ? frame->height :
127  FFMIN((jobnr + 1) * slice_h, frame->height);
128  int i, j, plane;
129 
130  for (plane = 1; plane < 3; plane++) {
131  for (i = slice_start; i < slice_end; i++) {
132  uint8_t *p = frame->data[plane] + (i >> s->vsub) * frame->linesize[plane];
133  for (j = 0; j < frame->width >> s->hsub; j++) {
134  /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
135  * representation of 128.5. The .5 is for rounding
136  * purposes. */
137  *p = ((*p - 128) * s->factor + 8421367) >> 16;
138  p++;
139  }
140  }
141  }
142 
143  return 0;
144 }
145 
146 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
147 {
148  AVFilterContext *ctx = inlink->dst;
149  FadeContext *s = ctx->priv;
150 
151  if (s->factor < UINT16_MAX) {
152  /* luma or rgb plane */
153  ctx->internal->execute(ctx, filter_slice_luma, frame, NULL,
154  FFMIN(frame->height, ctx->graph->nb_threads));
155 
156  if (frame->data[1] && frame->data[2]) {
157  /* chroma planes */
158  ctx->internal->execute(ctx, filter_slice_chroma, frame, NULL,
159  FFMIN(frame->height, ctx->graph->nb_threads));
160  }
161  }
162 
163  if (s->frame_index >= s->start_frame &&
164  s->frame_index <= s->stop_frame)
165  s->factor += s->fade_per_frame;
166  s->factor = av_clip_uint16(s->factor);
167  s->frame_index++;
168 
169  return ff_filter_frame(inlink->dst->outputs[0], frame);
170 }
171 
172 #define OFFSET(x) offsetof(FadeContext, x)
173 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
174 static const AVOption options[] = {
175  { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
176  { "in", "fade-in", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN }, .unit = "type" },
177  { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
178  { "start_frame", "Number of the first frame to which to apply the effect.",
179  OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
180  { "nb_frames", "Number of frames to which the effect should be applied.",
181  OFFSET(nb_frames), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, FLAGS },
182  { NULL },
183 };
184 
185 static const AVClass fade_class = {
186  .class_name = "fade",
187  .item_name = av_default_item_name,
188  .option = options,
189  .version = LIBAVUTIL_VERSION_INT,
190 };
191 
193  {
194  .name = "default",
195  .type = AVMEDIA_TYPE_VIDEO,
196  .config_props = config_props,
197  .get_video_buffer = ff_null_get_video_buffer,
198  .filter_frame = filter_frame,
199  .needs_writable = 1,
200  },
201  { NULL }
202 };
203 
205  {
206  .name = "default",
207  .type = AVMEDIA_TYPE_VIDEO,
208  },
209  { NULL }
210 };
211 
213  .name = "fade",
214  .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
215  .init = init,
216  .priv_size = sizeof(FadeContext),
217  .priv_class = &fade_class,
219 
220  .inputs = avfilter_vf_fade_inputs,
221  .outputs = avfilter_vf_fade_outputs,
223 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
static const AVFilterPad avfilter_vf_fade_outputs[]
Definition: vf_fade.c:204
AVOption.
Definition: opt.h:234
int bpp
Definition: vf_fade.c:45
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:1571
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
static int config_props(AVFilterLink *inlink)
Definition: vf_fade.c:83
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
#define FFALIGN(x, a)
Definition: common.h:62
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:586
const char * name
Pad name.
Definition: internal.h:42
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:733
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
#define FLAGS
Definition: vf_fade.c:173
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:103
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:976
static int flags
Definition: log.c:44
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:379
static const AVOption options[]
Definition: vf_fade.c:174
A filter pad used for either input or output.
Definition: internal.h:36
static int query_formats(AVFilterContext *ctx)
Definition: vf_fade.c:68
int width
width and height of the video frame
Definition: frame.h:174
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
static av_cold int init(AVFilterContext *ctx)
Definition: vf_fade.c:48
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void * priv
private data for use by the filter
Definition: avfilter.h:584
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:415
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fade.c:146
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
int fade_per_frame
Definition: vf_fade.c:42
#define FFMIN(a, b)
Definition: common.h:57
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
static int filter_slice_chroma(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_fade.c:119
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static const AVFilterPad avfilter_vf_fade_inputs[]
Definition: vf_fade.c:192
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
if(ac->has_optimized_func)
#define FADE_OUT
Definition: vf_fade.c:37
AVFilter ff_vf_fade
Definition: vf_fade.c:212
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
int start_frame
Definition: vf_fade.c:43
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
static int filter_slice_luma(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_fade.c:95
int vsub
Definition: vf_fade.c:45
Filter definition.
Definition: avfilter.h:421
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
const char * name
Filter name.
Definition: avfilter.h:425
int type
Definition: vf_fade.c:41
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
#define FADE_IN
Definition: vf_fade.c:36
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:609
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal and external API header
unsigned int stop_frame
Definition: vf_fade.c:44
int nb_frames
Definition: vf_fade.c:43
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
#define OFFSET(x)
Definition: vf_fade.c:172
avfilter_execute_func * execute
Definition: internal.h:137
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2017
int factor
Definition: vf_fade.c:42
int hsub
Definition: vf_fade.c:45
An instance of a filter.
Definition: avfilter.h:563
int height
Definition: frame.h:174
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:102
static const AVClass fade_class
Definition: vf_fade.c:185
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
for(j=16;j >0;--j)
unsigned int frame_index
Definition: vf_fade.c:44