]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_slicify.c
overlay: clear cur_buf on main input link.
[ffmpeg] / libavfilter / vf_slicify.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * video slicing filter
24  */
25
26 #include "avfilter.h"
27 #include "internal.h"
28 #include "video.h"
29 #include "libavutil/pixdesc.h"
30
31 typedef struct {
32     int h;          ///< output slice height
33     int vshift;     ///< vertical chroma subsampling shift
34     uint32_t lcg_state; ///< LCG state used to compute random slice height
35     int use_random_h;   ///< enable the use of random slice height values
36 } SliceContext;
37
38 static av_cold int init(AVFilterContext *ctx, const char *args)
39 {
40     SliceContext *slice = ctx->priv;
41
42     slice->h = 16;
43     if (args) {
44         if (!strcmp(args, "random")) {
45             slice->use_random_h = 1;
46         } else {
47             sscanf(args, "%d", &slice->h);
48         }
49     }
50     return 0;
51 }
52
53 static int config_props(AVFilterLink *link)
54 {
55     SliceContext *slice = link->dst->priv;
56
57     slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
58
59     return 0;
60 }
61
62 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
63 {
64     SliceContext *slice = link->dst->priv;
65
66     if (slice->use_random_h) {
67         slice->lcg_state = slice->lcg_state * 1664525 + 1013904223;
68         slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX;
69     }
70
71     /* ensure that slices play nice with chroma subsampling, and enforce
72      * a reasonable minimum size for the slices */
73     slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
74
75     av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h);
76     link->cur_buf = NULL;
77
78     ff_start_frame(link->dst->outputs[0], picref);
79 }
80
81 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
82 {
83     SliceContext *slice = link->dst->priv;
84     int y2;
85
86     if (slice_dir == 1) {
87         for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h)
88             ff_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
89
90         if (y2 < y + h)
91             ff_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
92     } else if (slice_dir == -1) {
93         for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h)
94             ff_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
95
96         if (y2 > y)
97             ff_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
98     }
99 }
100
101 AVFilter avfilter_vf_slicify = {
102     .name      = "slicify",
103     .description = NULL_IF_CONFIG_SMALL("Pass the images of input video on to next video filter as multiple slices."),
104
105     .init      = init,
106
107     .priv_size = sizeof(SliceContext),
108
109     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
110                                     .type             = AVMEDIA_TYPE_VIDEO,
111                                     .get_video_buffer = ff_null_get_video_buffer,
112                                     .start_frame      = start_frame,
113                                     .draw_slice       = draw_slice,
114                                     .config_props     = config_props,
115                                     .end_frame        = ff_null_end_frame, },
116                                   { .name = NULL}},
117     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
118                                     .type            = AVMEDIA_TYPE_VIDEO, },
119                                   { .name = NULL}},
120 };