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/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef struct {
36  int factor, fade_per_frame;
37  unsigned int frame_index, start_frame, stop_frame;
38  int hsub, vsub, bpp;
39 } FadeContext;
40 
41 static av_cold int init(AVFilterContext *ctx, const char *args)
42 {
43  FadeContext *fade = ctx->priv;
44  unsigned int nb_frames;
45  char in_out[4];
46 
47  if (!args ||
48  sscanf(args, " %3[^:]:%u:%u", in_out, &fade->start_frame, &nb_frames) != 3) {
49  av_log(ctx, AV_LOG_ERROR,
50  "Expected 3 arguments '(in|out):#:#':'%s'\n", args);
51  return AVERROR(EINVAL);
52  }
53 
54  nb_frames = nb_frames ? nb_frames : 1;
55  fade->fade_per_frame = (1 << 16) / nb_frames;
56  if (!strcmp(in_out, "in"))
57  fade->factor = 0;
58  else if (!strcmp(in_out, "out")) {
59  fade->fade_per_frame = -fade->fade_per_frame;
60  fade->factor = (1 << 16);
61  } else {
62  av_log(ctx, AV_LOG_ERROR,
63  "first argument must be 'in' or 'out':'%s'\n", in_out);
64  return AVERROR(EINVAL);
65  }
66  fade->stop_frame = fade->start_frame + nb_frames;
67 
69  "type:%s start_frame:%d nb_frames:%d\n",
70  in_out, fade->start_frame, nb_frames);
71  return 0;
72 }
73 
75 {
76  static const enum AVPixelFormat pix_fmts[] = {
83  };
84 
86  return 0;
87 }
88 
89 static int config_props(AVFilterLink *inlink)
90 {
91  FadeContext *fade = inlink->dst->priv;
92  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
93 
94  fade->hsub = pixdesc->log2_chroma_w;
95  fade->vsub = pixdesc->log2_chroma_h;
96 
97  fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
98  return 0;
99 }
100 
101 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
102 {
103  FadeContext *fade = inlink->dst->priv;
104  uint8_t *p;
105  int i, j, plane;
106 
107  if (fade->factor < UINT16_MAX) {
108  /* luma or rgb plane */
109  for (i = 0; i < frame->video->h; i++) {
110  p = frame->data[0] + i * frame->linesize[0];
111  for (j = 0; j < inlink->w * fade->bpp; j++) {
112  /* fade->factor is using 16 lower-order bits for decimal
113  * places. 32768 = 1 << 15, it is an integer representation
114  * of 0.5 and is for rounding. */
115  *p = (*p * fade->factor + 32768) >> 16;
116  p++;
117  }
118  }
119 
120  if (frame->data[1] && frame->data[2]) {
121  /* chroma planes */
122  for (plane = 1; plane < 3; plane++) {
123  for (i = 0; i < frame->video->h; i++) {
124  p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
125  for (j = 0; j < inlink->w >> fade->hsub; j++) {
126  /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
127  * representation of 128.5. The .5 is for rounding
128  * purposes. */
129  *p = ((*p - 128) * fade->factor + 8421367) >> 16;
130  p++;
131  }
132  }
133  }
134  }
135  }
136 
137  if (fade->frame_index >= fade->start_frame &&
138  fade->frame_index <= fade->stop_frame)
139  fade->factor += fade->fade_per_frame;
140  fade->factor = av_clip_uint16(fade->factor);
141  fade->frame_index++;
142 
143  return ff_filter_frame(inlink->dst->outputs[0], frame);
144 }
145 
147  {
148  .name = "default",
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,
155  },
156  { NULL }
157 };
158 
160  {
161  .name = "default",
162  .type = AVMEDIA_TYPE_VIDEO,
163  },
164  { NULL }
165 };
166 
168  .name = "fade",
169  .description = NULL_IF_CONFIG_SMALL("Fade in/out input video"),
170  .init = init,
171  .priv_size = sizeof(FadeContext),
173 
174  .inputs = avfilter_vf_fade_inputs,
175  .outputs = avfilter_vf_fade_outputs,
176 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
static const AVFilterPad avfilter_vf_fade_outputs[]
Definition: vf_fade.c:159
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int bpp
Definition: vf_fade.c:38
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
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:122
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:1408
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:459
static int config_props(AVFilterLink *inlink)
Definition: vf_fade.c:89
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
static av_cold int init(AVFilterContext *ctx, const char *args)
Definition: vf_fade.c:41
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
const char * name
Pad name.
Definition: internal.h:39
uint8_t
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:101
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
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:375
A filter pad used for either input or output.
Definition: internal.h:33
static int query_formats(AVFilterContext *ctx)
Definition: vf_fade.c:74
int h
image height
Definition: avfilter.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void * priv
private data for use by the filter
Definition: avfilter.h:439
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.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:36
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
static const AVFilterPad avfilter_vf_fade_inputs[]
Definition: vf_fade.c:146
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
Definition: vf_fade.c:101
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
AVFilter avfilter_vf_fade
Definition: vf_fade.c:167
static int start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: dxva2_h264.c:370
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
int vsub
Definition: vf_fade.c:38
Filter definition.
Definition: avfilter.h:371
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:110
unsigned int start_frame
Definition: vf_fade.c:37
const char * name
filter name
Definition: avfilter.h:372
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
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:37
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 AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
int factor
Definition: vf_fade.c:36
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int hsub
Definition: vf_fade.c:38
An instance of a filter.
Definition: avfilter.h:418
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
for(j=16;j >0;--j)
unsigned int frame_index
Definition: vf_fade.c:37
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)