00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/common.h"
00030 #include "libavutil/eval.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "avfilter.h"
00034 #include "drawutils.h"
00035 #include "internal.h"
00036 #include "formats.h"
00037 #include "internal.h"
00038 #include "video.h"
00039
00040 #define R 0
00041 #define G 1
00042 #define B 2
00043 #define A 3
00044
00045 #define Y 0
00046 #define U 1
00047 #define V 2
00048
00049 typedef struct {
00050 const AVClass *class;
00051 int factor, fade_per_frame;
00052 unsigned int frame_index, start_frame, stop_frame, nb_frames;
00053 int hsub, vsub, bpp;
00054 unsigned int black_level, black_level_scaled;
00055 uint8_t is_packed_rgb;
00056 uint8_t rgba_map[4];
00057 int alpha;
00058
00059 char *type;
00060 } FadeContext;
00061
00062 #define OFFSET(x) offsetof(FadeContext, x)
00063 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00064
00065 static const AVOption fade_options[] = {
00066 { "type", "set the fade direction", OFFSET(type), AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX, FLAGS },
00067 { "t", "set the fade direction", OFFSET(type), AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX, FLAGS },
00068 { "start_frame", "set expression of frame to start fading", OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, FLAGS },
00069 { "s", "set expression of frame to start fading", OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, FLAGS },
00070 { "nb_frames", "set expression for fade duration in frames", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64 = 25 }, 0, INT_MAX, FLAGS },
00071 { "n", "set expression for fade duration in frames", OFFSET(nb_frames), AV_OPT_TYPE_INT, {.i64 = 25 }, 0, INT_MAX, FLAGS },
00072 { "alpha", "fade alpha if it is available on the input", OFFSET(alpha), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, FLAGS },
00073 {NULL},
00074 };
00075
00076 AVFILTER_DEFINE_CLASS(fade);
00077
00078 static av_cold int init(AVFilterContext *ctx, const char *args)
00079 {
00080 FadeContext *fade = ctx->priv;
00081 int ret = 0;
00082 char *args1, *expr, *bufptr = NULL;
00083
00084 fade->class = &fade_class;
00085 av_opt_set_defaults(fade);
00086
00087 if (!(args1 = av_strdup(args))) {
00088 ret = AVERROR(ENOMEM);
00089 goto end;
00090 }
00091
00092 if (expr = av_strtok(args1, ":", &bufptr)) {
00093 av_free(fade->type);
00094 if (!(fade->type = av_strdup(expr))) {
00095 ret = AVERROR(ENOMEM);
00096 goto end;
00097 }
00098 }
00099 if (expr = av_strtok(NULL, ":", &bufptr)) {
00100 if ((ret = av_opt_set(fade, "start_frame", expr, 0)) < 0) {
00101 av_log(ctx, AV_LOG_ERROR,
00102 "Invalid value '%s' for start_frame option\n", expr);
00103 return ret;
00104 }
00105 }
00106 if (expr = av_strtok(NULL, ":", &bufptr)) {
00107 if ((ret = av_opt_set(fade, "nb_frames", expr, 0)) < 0) {
00108 av_log(ctx, AV_LOG_ERROR,
00109 "Invalid value '%s' for nb_frames option\n", expr);
00110 return ret;
00111 }
00112 }
00113
00114 if (bufptr && (ret = av_set_options_string(fade, bufptr, "=", ":")) < 0)
00115 goto end;
00116
00117 fade->fade_per_frame = (1 << 16) / fade->nb_frames;
00118 if (!strcmp(fade->type, "in"))
00119 fade->factor = 0;
00120 else if (!strcmp(fade->type, "out")) {
00121 fade->fade_per_frame = -fade->fade_per_frame;
00122 fade->factor = (1 << 16);
00123 } else {
00124 av_log(ctx, AV_LOG_ERROR,
00125 "Type argument must be 'in' or 'out' but '%s' was specified\n", fade->type);
00126 ret = AVERROR(EINVAL);
00127 goto end;
00128 }
00129 fade->stop_frame = fade->start_frame + fade->nb_frames;
00130
00131 av_log(ctx, AV_LOG_VERBOSE,
00132 "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
00133 fade->type, fade->start_frame, fade->nb_frames, fade->alpha);
00134
00135 end:
00136 av_free(args1);
00137 return ret;
00138 }
00139
00140 static av_cold void uninit(AVFilterContext *ctx)
00141 {
00142 FadeContext *fade = ctx->priv;
00143
00144 av_freep(&fade->type);
00145 }
00146
00147 static int query_formats(AVFilterContext *ctx)
00148 {
00149 static const enum PixelFormat pix_fmts[] = {
00150 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
00151 PIX_FMT_YUV411P, PIX_FMT_YUV410P,
00152 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
00153 PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
00154 PIX_FMT_YUVA420P,
00155 PIX_FMT_RGB24, PIX_FMT_BGR24,
00156 PIX_FMT_ARGB, PIX_FMT_ABGR,
00157 PIX_FMT_RGBA, PIX_FMT_BGRA,
00158 PIX_FMT_NONE
00159 };
00160
00161 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00162 return 0;
00163 }
00164
00165 const static enum PixelFormat studio_level_pix_fmts[] = {
00166 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
00167 PIX_FMT_YUV411P, PIX_FMT_YUV410P,
00168 PIX_FMT_YUV440P,
00169 PIX_FMT_NONE
00170 };
00171
00172 static enum PixelFormat alpha_pix_fmts[] = {
00173 PIX_FMT_YUVA420P,
00174 PIX_FMT_ARGB, PIX_FMT_ABGR,
00175 PIX_FMT_RGBA, PIX_FMT_BGRA,
00176 PIX_FMT_NONE
00177 };
00178
00179 static int config_props(AVFilterLink *inlink)
00180 {
00181 FadeContext *fade = inlink->dst->priv;
00182 const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
00183
00184 fade->hsub = pixdesc->log2_chroma_w;
00185 fade->vsub = pixdesc->log2_chroma_h;
00186
00187 fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
00188 fade->alpha = fade->alpha ? ff_fmt_is_in(inlink->format, alpha_pix_fmts) : 0;
00189 fade->is_packed_rgb = ff_fill_rgba_map(fade->rgba_map, inlink->format) >= 0;
00190
00191
00192 fade->black_level =
00193 ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !fade->alpha ? 16 : 0;
00194
00195
00196 fade->black_level_scaled = (fade->black_level << 16) + 32768;
00197 return 0;
00198 }
00199
00200 static void fade_plane(int y, int h, int w,
00201 int fade_factor, int black_level, int black_level_scaled,
00202 uint8_t offset, uint8_t step, int bytes_per_plane,
00203 uint8_t *data, int line_size)
00204 {
00205 uint8_t *p;
00206 int i, j;
00207
00208
00209 for (i = 0; i < h; i++) {
00210 p = data + offset + (y+i) * line_size;
00211 for (j = 0; j < w * bytes_per_plane; j++) {
00212
00213 *p = ((*p - black_level) * fade_factor + black_level_scaled) >> 16;
00214 p+=step;
00215 }
00216 }
00217 }
00218
00219 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00220 {
00221 FadeContext *fade = inlink->dst->priv;
00222 AVFilterBufferRef *outpic = inlink->cur_buf;
00223 uint8_t *p;
00224 int i, j, plane;
00225
00226 if (fade->factor < UINT16_MAX) {
00227 if (fade->alpha) {
00228
00229 plane = fade->is_packed_rgb ? 0 : A;
00230
00231 fade_plane(y, h, inlink->w,
00232 fade->factor, fade->black_level, fade->black_level_scaled,
00233 fade->is_packed_rgb ? fade->rgba_map[A] : 0,
00234 fade->is_packed_rgb ? 4 : 1,
00235 1, outpic->data[plane], outpic->linesize[plane]);
00236 } else {
00237
00238 fade_plane(y, h, inlink->w,
00239 fade->factor, fade->black_level, fade->black_level_scaled,
00240 0, 1,
00241 fade->bpp, outpic->data[0], outpic->linesize[0]);
00242 if (outpic->data[1] && outpic->data[2]) {
00243
00244 for (plane = 1; plane < 3; plane++) {
00245 for (i = 0; i < h; i++) {
00246 p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
00247 for (j = 0; j < inlink->w >> fade->hsub; j++) {
00248
00249
00250
00251 *p = ((*p - 128) * fade->factor + 8421367) >> 16;
00252 p++;
00253 }
00254 }
00255 }
00256 }
00257 }
00258 }
00259
00260 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00261 }
00262
00263 static int end_frame(AVFilterLink *inlink)
00264 {
00265 FadeContext *fade = inlink->dst->priv;
00266 int ret;
00267
00268 ret = ff_end_frame(inlink->dst->outputs[0]);
00269
00270 if (fade->frame_index >= fade->start_frame &&
00271 fade->frame_index <= fade->stop_frame)
00272 fade->factor += fade->fade_per_frame;
00273 fade->factor = av_clip_uint16(fade->factor);
00274 fade->frame_index++;
00275
00276 return ret;
00277 }
00278
00279 AVFilter avfilter_vf_fade = {
00280 .name = "fade",
00281 .description = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
00282 .init = init,
00283 .uninit = uninit,
00284 .priv_size = sizeof(FadeContext),
00285 .query_formats = query_formats,
00286
00287 .inputs = (const AVFilterPad[]) {{ .name = "default",
00288 .type = AVMEDIA_TYPE_VIDEO,
00289 .config_props = config_props,
00290 .get_video_buffer = ff_null_get_video_buffer,
00291 .start_frame = ff_null_start_frame,
00292 .draw_slice = draw_slice,
00293 .end_frame = end_frame,
00294 .min_perms = AV_PERM_READ | AV_PERM_WRITE },
00295 { .name = NULL}},
00296 .outputs = (const AVFilterPad[]) {{ .name = "default",
00297 .type = AVMEDIA_TYPE_VIDEO, },
00298 { .name = NULL}},
00299 .priv_class = &fade_class,
00300 };