]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_slicify.c
31d74979af97c060cb10764c7e32fb7f4bd35d10
[ffmpeg] / libavfilter / vf_slicify.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 "video.h"
28 #include "libavutil/pixdesc.h"
29
30 typedef struct {
31     int h;          ///< output slice height
32     int vshift;     ///< vertical chroma subsampling shift
33     uint32_t lcg_state; ///< LCG state used to compute random slice height
34     int use_random_h;   ///< enable the use of random slice height values
35 } SliceContext;
36
37 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
38 {
39     SliceContext *slice = ctx->priv;
40
41     slice->h = 16;
42     if (args) {
43         if (!strcmp(args, "random")) {
44             slice->use_random_h = 1;
45         } else {
46             sscanf(args, "%d", &slice->h);
47         }
48     }
49     return 0;
50 }
51
52 static int config_props(AVFilterLink *link)
53 {
54     SliceContext *slice = link->dst->priv;
55
56     slice->vshift = av_pix_fmt_descriptors[link->format].log2_chroma_h;
57
58     return 0;
59 }
60
61 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
62 {
63     SliceContext *slice = link->dst->priv;
64
65     if (slice->use_random_h) {
66         slice->lcg_state = slice->lcg_state * 1664525 + 1013904223;
67         slice->h = 8 + (uint64_t)slice->lcg_state * 25 / UINT32_MAX;
68     }
69
70     /* ensure that slices play nice with chroma subsampling, and enforce
71      * a reasonable minimum size for the slices */
72     slice->h = FFMAX(8, slice->h & (-1 << slice->vshift));
73
74     av_log(link->dst, AV_LOG_DEBUG, "h:%d\n", slice->h);
75
76     avfilter_start_frame(link->dst->outputs[0], picref);
77 }
78
79 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
80 {
81     SliceContext *slice = link->dst->priv;
82     int y2;
83
84     if (slice_dir == 1) {
85         for (y2 = y; y2 + slice->h <= y + h; y2 += slice->h)
86             avfilter_draw_slice(link->dst->outputs[0], y2, slice->h, slice_dir);
87
88         if (y2 < y + h)
89             avfilter_draw_slice(link->dst->outputs[0], y2, y + h - y2, slice_dir);
90     } else if (slice_dir == -1) {
91         for (y2 = y + h; y2 - slice->h >= y; y2 -= slice->h)
92             avfilter_draw_slice(link->dst->outputs[0], y2 - slice->h, slice->h, slice_dir);
93
94         if (y2 > y)
95             avfilter_draw_slice(link->dst->outputs[0], y, y2 - y, slice_dir);
96     }
97 }
98
99 AVFilter avfilter_vf_slicify = {
100     .name      = "slicify",
101     .description = NULL_IF_CONFIG_SMALL("Pass the images of input video on to next video filter as multiple slices."),
102
103     .init      = init,
104
105     .priv_size = sizeof(SliceContext),
106
107     .inputs    = (AVFilterPad[]) {{ .name             = "default",
108                                     .type             = AVMEDIA_TYPE_VIDEO,
109                                     .get_video_buffer = ff_null_get_video_buffer,
110                                     .start_frame      = start_frame,
111                                     .draw_slice       = draw_slice,
112                                     .config_props     = config_props,
113                                     .end_frame        = ff_null_end_frame, },
114                                   { .name = NULL}},
115     .outputs   = (AVFilterPad[]) {{ .name            = "default",
116                                     .type            = AVMEDIA_TYPE_VIDEO, },
117                                   { .name = NULL}},
118 };