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