libavfilter/vf_fade.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Brandon Mintern
00003  * Copyright (c) 2007 Bobby Bingham
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/pixdesc.h"
00032 #include "avfilter.h"
00033 #include "drawutils.h"
00034 #include "internal.h"
00035 
00036 #define R 0
00037 #define G 1
00038 #define B 2
00039 #define A 3
00040 
00041 #define Y 0
00042 #define U 1
00043 #define V 2
00044 
00045 typedef struct {
00046     const AVClass *class;
00047     int factor, fade_per_frame;
00048     unsigned int frame_index, start_frame, stop_frame, nb_frames;
00049     int hsub, vsub, bpp;
00050     unsigned int black_level, black_level_scaled;
00051     uint8_t is_packed_rgb;
00052     uint8_t rgba_map[4];
00053     int alpha;
00054 
00055     char *type;
00056 } FadeContext;
00057 
00058 #define OFFSET(x) offsetof(FadeContext, x)
00059 
00060 static const AVOption fade_options[] = {
00061     { "type",        "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
00062     { "t",           "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX },
00063     { "start_frame", "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0    }, 0, INT_MAX },
00064     { "s",           "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.dbl = 0    }, 0, INT_MAX },
00065     { "nb_frames",   "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.dbl = 25   }, 0, INT_MAX },
00066     { "n",           "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.dbl = 25   }, 0, INT_MAX },
00067     { "alpha",       "fade alpha if it is available on the input", OFFSET(alpha),       AV_OPT_TYPE_INT, {.dbl = 0    }, 0,       1 },
00068     {NULL},
00069 };
00070 
00071 static const char *fade_get_name(void *ctx)
00072 {
00073     return "fade";
00074 }
00075 
00076 static const AVClass fade_class = {
00077     "FadeContext",
00078     fade_get_name,
00079     fade_options
00080 };
00081 
00082 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00083 {
00084     FadeContext *fade = ctx->priv;
00085     int ret = 0;
00086     char *args1, *expr, *bufptr = NULL;
00087 
00088     fade->class = &fade_class;
00089     av_opt_set_defaults(fade);
00090 
00091     if (!(args1 = av_strdup(args))) {
00092         ret = AVERROR(ENOMEM);
00093         goto end;
00094     }
00095 
00096     if (expr = av_strtok(args1, ":", &bufptr)) {
00097         if (!(fade->type = av_strdup(expr))) {
00098             ret = AVERROR(ENOMEM);
00099             goto end;
00100         }
00101     }
00102     if (expr = av_strtok(NULL, ":", &bufptr)) {
00103         if ((ret = av_opt_set(fade, "start_frame", expr, 0)) < 0) {
00104             av_log(ctx, AV_LOG_ERROR,
00105                    "Invalid value '%s' for start_frame option\n", expr);
00106             goto end;
00107         }
00108     }
00109     if (expr = av_strtok(NULL, ":", &bufptr)) {
00110         if ((ret = av_opt_set(fade, "nb_frames", expr, 0)) < 0) {
00111             av_log(ctx, AV_LOG_ERROR,
00112                    "Invalid value '%s' for nb_frames option\n", expr);
00113             goto end;
00114         }
00115     }
00116 
00117     if (bufptr && (ret = av_set_options_string(fade, bufptr, "=", ":")) < 0)
00118         goto end;
00119 
00120     fade->fade_per_frame = (1 << 16) / fade->nb_frames;
00121     if (!strcmp(fade->type, "in"))
00122         fade->factor = 0;
00123     else if (!strcmp(fade->type, "out")) {
00124         fade->fade_per_frame = -fade->fade_per_frame;
00125         fade->factor = (1 << 16);
00126     } else {
00127         av_log(ctx, AV_LOG_ERROR,
00128                "Type argument must be 'in' or 'out' but '%s' was specified\n", fade->type);
00129         ret = AVERROR(EINVAL);
00130         goto end;
00131     }
00132     fade->stop_frame = fade->start_frame + fade->nb_frames;
00133 
00134     av_log(ctx, AV_LOG_INFO,
00135            "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
00136            fade->type, fade->start_frame, fade->nb_frames, fade->alpha);
00137 
00138 end:
00139     av_free(args1);
00140     return ret;
00141 }
00142 
00143 static av_cold void uninit(AVFilterContext *ctx)
00144 {
00145     FadeContext *fade = ctx->priv;
00146 
00147     av_freep(&fade->type);
00148 }
00149 
00150 static int query_formats(AVFilterContext *ctx)
00151 {
00152     static const enum PixelFormat pix_fmts[] = {
00153         PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
00154         PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
00155         PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
00156         PIX_FMT_YUV440P,  PIX_FMT_YUVJ440P,
00157         PIX_FMT_YUVA420P,
00158         PIX_FMT_RGB24,    PIX_FMT_BGR24,
00159         PIX_FMT_ARGB,     PIX_FMT_ABGR,
00160         PIX_FMT_RGBA,     PIX_FMT_BGRA,
00161         PIX_FMT_NONE
00162     };
00163 
00164     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00165     return 0;
00166 }
00167 
00168 const static enum PixelFormat studio_level_pix_fmts[] = {
00169     PIX_FMT_YUV444P,  PIX_FMT_YUV422P,  PIX_FMT_YUV420P,
00170     PIX_FMT_YUV411P,  PIX_FMT_YUV410P,
00171     PIX_FMT_YUV440P,
00172     PIX_FMT_NONE
00173 };
00174 
00175 static enum PixelFormat alpha_pix_fmts[] = {
00176     PIX_FMT_YUVA420P,
00177     PIX_FMT_ARGB, PIX_FMT_ABGR,
00178     PIX_FMT_RGBA, PIX_FMT_BGRA,
00179     PIX_FMT_NONE
00180 };
00181 
00182 static int config_props(AVFilterLink *inlink)
00183 {
00184     FadeContext *fade = inlink->dst->priv;
00185     const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[inlink->format];
00186 
00187     fade->hsub = pixdesc->log2_chroma_w;
00188     fade->vsub = pixdesc->log2_chroma_h;
00189 
00190     fade->bpp = av_get_bits_per_pixel(pixdesc) >> 3;
00191     fade->alpha = fade->alpha ? ff_fmt_is_in(inlink->format, alpha_pix_fmts) : 0;
00192     fade->is_packed_rgb = ff_fill_rgba_map(fade->rgba_map, inlink->format) >= 0;
00193 
00194     /* use CCIR601/709 black level for studio-level pixel non-alpha components */
00195     fade->black_level =
00196             ff_fmt_is_in(inlink->format, studio_level_pix_fmts) && !fade->alpha ? 16 : 0;
00197     /* 32768 = 1 << 15, it is an integer representation
00198      * of 0.5 and is for rounding. */
00199     fade->black_level_scaled = (fade->black_level << 16) + 32768;
00200     return 0;
00201 }
00202 
00203 static void fade_plane(int y, int h, int w,
00204                        int fade_factor, int black_level, int black_level_scaled,
00205                        uint8_t offset, uint8_t step, int bytes_per_plane,
00206                        uint8_t *data, int line_size)
00207 {
00208     uint8_t *p;
00209     int i, j;
00210 
00211     /* luma, alpha or rgb plane */
00212     for (i = 0; i < h; i++) {
00213         p = data + offset + (y+i) * line_size;
00214         for (j = 0; j < w * bytes_per_plane; j++) {
00215             /* fade->factor is using 16 lower-order bits for decimal places. */
00216             *p = ((*p - black_level) * fade_factor + black_level_scaled) >> 16;
00217             p+=step;
00218         }
00219     }
00220 }
00221 
00222 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00223 {
00224     FadeContext *fade = inlink->dst->priv;
00225     AVFilterBufferRef *outpic = inlink->cur_buf;
00226     uint8_t *p;
00227     int i, j, plane;
00228 
00229     if (fade->factor < UINT16_MAX) {
00230         if (fade->alpha) {
00231             // alpha only
00232             plane = fade->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats
00233                                                  // or plane 3 for planar formats
00234             fade_plane(y, h, inlink->w,
00235                        fade->factor, fade->black_level, fade->black_level_scaled,
00236                        fade->is_packed_rgb ? fade->rgba_map[A] : 0, // alpha offset for packed formats
00237                        fade->is_packed_rgb ? 4 : 1,                 // pixstep for 8 bit packed formats
00238                        1, outpic->data[plane], outpic->linesize[plane]);
00239         } else {
00240             /* luma or rgb plane */
00241             fade_plane(y, h, inlink->w,
00242                        fade->factor, fade->black_level, fade->black_level_scaled,
00243                        0, 1, // offset & pixstep for Y plane or RGB packed format
00244                        fade->bpp, outpic->data[0], outpic->linesize[0]);
00245             if (outpic->data[1] && outpic->data[2]) {
00246                 /* chroma planes */
00247                 for (plane = 1; plane < 3; plane++) {
00248                     for (i = 0; i < h; i++) {
00249                         p = outpic->data[plane] + ((y+i) >> fade->vsub) * outpic->linesize[plane];
00250                         for (j = 0; j < inlink->w >> fade->hsub; j++) {
00251                             /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
00252                              * representation of 128.5. The .5 is for rounding
00253                              * purposes. */
00254                             *p = ((*p - 128) * fade->factor + 8421367) >> 16;
00255                             p++;
00256                         }
00257                     }
00258                 }
00259             }
00260         }
00261     }
00262 
00263     avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00264 }
00265 
00266 static void end_frame(AVFilterLink *inlink)
00267 {
00268     FadeContext *fade = inlink->dst->priv;
00269 
00270     avfilter_end_frame(inlink->dst->outputs[0]);
00271 
00272     if (fade->frame_index >= fade->start_frame &&
00273         fade->frame_index <= fade->stop_frame)
00274         fade->factor += fade->fade_per_frame;
00275     fade->factor = av_clip_uint16(fade->factor);
00276     fade->frame_index++;
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 = avfilter_null_get_video_buffer,
00291                                     .start_frame      = avfilter_null_start_frame,
00292                                     .draw_slice      = draw_slice,
00293                                     .end_frame       = end_frame,
00294                                     .min_perms       = AV_PERM_READ | AV_PERM_WRITE,
00295                                     .rej_perms       = AV_PERM_PRESERVE, },
00296                                   { .name = NULL}},
00297     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
00298                                     .type            = AVMEDIA_TYPE_VIDEO, },
00299                                   { .name = NULL}},
00300 };